Stefano Crocco
1/4/2008 4:16:00 PM
Alle venerd=EC 4 gennaio 2008, AJS ha scritto:
> title=3D"Title"
> subtitle=3D"Subtitle"
> body=3D"Body"
> [title,subtitle,body].each {|item| item.downcase!}
>
> This successfully sets the original variables to "title", "subtitle",
> "body"
>
> BUT
>
> [title,subtitle,body].each {|item| item=3D""}
>
> has no effect.
>
> Why doesn't this work? How can I make it work?
>
> thanks
The block variable (item) is set by ruby to "point to" each object of the=20
array in turn. String#downcase! is a destructive method: it changes its=20
receiver, so the elements of the original array get changed. On the other=20
hand, when you do item =3D "", you're telling the variable itewm to "point =
to"=20
another object, a new (empty) string. This doesn't modify the original arra=
y=20
at all.
If you want to change the contents of the original array, your should use=20
Array#map!:
[title,subtitle,body].map! {|item| ""}
Array#map! replaces each element of the array with the return value of the=
=20
block.
I hope this helps
Stefano