From: Matthew K. Williams on
On Wed, 5 May 2010, Matthew K. Williams wrote:

> On Wed, 5 May 2010, Brian Candler wrote:
>
>> Matthew K. Williams wrote:
>>> Now, you could say:
>>> [x, y].each {|v| v += z}
>>
>> Except that does nothing except calculate values and throw them away
>> (since v drops out of scope at the end of the block). x and y are
>> unchanged.
>>
>> Local variables are not objects, and you can't get "references" to them.
>> You'd have to do something awful with eval to get the effect you want:
>>
>> ["x", "y"].each { |v| eval "#{v} += z" }
>
> Good point.
>
> <MANTRA>
> I should not reply to mailing lists when I'm distracted by a coding problem
> in another language.
> </MANTRA>
>
> Back to 'bash'ing away....

I just had a semi-evil thought, however.....

x,y = ([x,y].map{|v| v+=z})

At that point, however,

x+=z
y+=z

are much clearer ;-)

Matt

First  |  Prev  | 
Pages: 1 2 3
Prev: * splat error
Next: Calculation in a block