From: Sebastian Hungerecker Date: 2008-01-05T01:23:06+09:00 Subject: Re: iterating over array of variables - can't change them? AJS wrote: > title="Title" > subtitle="Subtitle" > body="Body" > [title,subtitle,body].each {|item| item.downcase!} > > This successfully sets the original variables to "title", "subtitle", > "body" No, it doesn't. It changes the strings, it doesn't do anything to the variables. The variables still point to the same object as before, it's just the objects that are different now. > [title,subtitle,body].each {|item| item=""} > > has no effect. > > Why doesn't this work? How can I make it work? It works just fine. It just doesn't do what you want it to. Here's what it does: It takes each of the elements in the array and then yields it. The code in the block will then run with item pointing to the element. After the assignment (item="") item will point to an empty string. This has no effect whatsoever on the variables title, subtitle and body. What you want is map instead of each (which gives you a new array containing the results of yielding each item) or String#replace instead of reassignment (which would change the object instead of the variable). HTH, Sebastian -- Jabber: sepp2k@jabber.org ICQ: 205544826