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

Tom Stuart tom at codon.com
Tue Jul 1 02:57:36 PDT 2014


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


More information about the Chat mailing list