[LRUG] Ruby question for a change! Why does this happen...

Ronny Ager-Wick ronny at ager-wick.com
Tue Jul 1 03:00:53 PDT 2014


On 01/07/14 17:57, Tom Stuart wrote:
> On 1 Jul 2014, at 10:52, Ronny Ager-Wick <ronny at ager-wick.com> wrote:
>> irb(main):001:0> def foo
>> irb(main):002:1>   @bar ||= []
>> irb(main):003:1> end
>> => nil
>> irb(main):004:0> def foo= obj
>> irb(main):005:1>   @bar = obj
>> irb(main):006:1> end
>> => nil
>> irb(main):007:0> foo
>> => []
>> irb(main):008:0> foo + ["a"]
>> => ["a"]
>> irb(main):009:0> foo = ["b"]
>> => ["b"]
>> irb(main):010:0> foo += ["c"]
>> => ["b", "c"]
> It hasn't actually worked, though. You've just created a local variable called `foo`:
>
>>> foo
> => ["b", "c"]
>>> @bar
> => []
>>> send :foo
> => []
>
> Cheers,
> -Tom
I stand corrected. How deceiving this terrain can be sometimes! :)



More information about the Chat mailing list