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

Ronny Ager-Wick ronny at ager-wick.com
Tue Jul 1 02:52:56 PDT 2014


I was just playing around in irb, and it seems to work... I didn't look into 
it theoretically, but when the map and the terrain don't match, I tend to 
trust the terrain.

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"]


On 01/07/14 17:46, Gabe da Silveira wrote:
> That will do absolutely nothing since he's not calling self.foo=
>
>
> On Tue, Jul 1, 2014 at 10:36 AM, Ronny Ager-Wick <ronny at ager-wick.com 
> <mailto:ronny at ager-wick.com>> wrote:
>
>     You don't have any definition for what should happen to foo when
>     assigning a value.
>     Try adding this:
>
>     def foo= obj
>       @b = obj
>     end
>
>
>
>     On 01/07/14 17:12, Kenneth Lee wrote:
>>
>>     Doing some lazy instantiation and come across this oddity, maybe I'm
>>     just being dense.
>>
>>     $ irb
>>
>>     irb(main):001:0> def foo
>>
>>     irb(main):002:1>   @bar ||= []
>>
>>     irb(main):003:1> end
>>
>>     => :foo
>>
>>     irb(main):004:0> foo
>>
>>     => []
>>
>>     irb(main):005:0> foo += []
>>
>>     NoMethodError: undefined method `+' for nil:NilClass
>>
>>     from (irb):5
>>
>>     from /Users/ken/.rbenv/versions/2.1.1/bin/irb:11:in `<main>'
>>
>>     irb(main):006:0> foo
>>
>>     => nil
>>
>>
>>
>>     _______________________________________________
>>     Chat mailing list
>>     Chat at lists.lrug.org  <mailto:Chat at lists.lrug.org>
>>     http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>
>
>     _______________________________________________
>     Chat mailing list
>     Chat at lists.lrug.org <mailto:Chat at lists.lrug.org>
>     http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>
>
>
>
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20140701/1f09c62a/attachment.html>


More information about the Chat mailing list