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

Kenneth Lee kenfodder at gmail.com
Tue Jul 1 02:12:50 PDT 2014


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20140701/29228a9b/attachment-0003.html>


More information about the Chat mailing list