From: Ken Bloom Date: 2008-01-06T09:40:11+09:00 Subject: Re: iterating over array of variables - can't change them? On Fri, 04 Jan 2008 21:00:56 +0100, Robert Klemme wrote: > On 04.01.2008 18:36, Ken Bloom wrote: >> On Fri, 04 Jan 2008 08:06:38 -0800, 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" >>> >>> BUT >>> >>> [title,subtitle,body].each {|item| item=""} >>> >>> has no effect. >>> >>> Why doesn't this work? How can I make it work? >>> >>> thanks >> >> Refer to the variables as strings and use eval. >> >> ["title","subtitle","body"].each {|item| eval "#{item}=''"} > > I believe this is rather inefficient (aka slow) and more importantly > will not run under all circumstances ($SAFE). Plus, it's not needed. > :-) Whether or not it's needed depends entirely on how much he's condensed the example to post it here. If he were interested in changing the class of object, this is the only technique that would update the variables automatically. (So I'm doing more than the minimum needed to make the test case work, so I'm not a real extreme programmer.) --Ken -- Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory. Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/