From: Stefano Crocco Date: 2008-01-05T01:16:12+09:00 Subject: Re: iterating over array of variables - can't change them? Alle venerd́ 4 gennaio 2008, AJS ha scritto: > title="Title" > subtitle="Subtitle" > body="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=""} > > 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 array in turn. String#downcase! is a destructive method: it changes its receiver, so the elements of the original array get changed. On the other hand, when you do item = "", you're telling the variable itewm to "point to" another object, a new (empty) string. This doesn't modify the original array at all. If you want to change the contents of the original array, your should use Array#map!: [title,subtitle,body].map! {|item| ""} Array#map! replaces each element of the array with the return value of the block. I hope this helps Stefano