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

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


You don't have any definition for what should happen to foo when assigning a 
value.
Try adding this:

def foo= obj
   @bar = 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
> 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/f86ff083/attachment-0003.html>


More information about the Chat mailing list