[LRUG] Ruby question for a change! Why does this happen...
Garry Shutler
garry at robustsoftware.co.uk
Tue Jul 1 02:17:47 PDT 2014
foo += []
Is shorthand for:
foo = foo + []
When you think of it like that, it's more obvious that foo has to be a new
variable rather than a call to the existing method.
*Garry Shutler*
@gshutler <http://twitter.com/gshutler>
gshutler.com
On 1 July 2014 10:12, Kenneth Lee <kenfodder at gmail.com> 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/71c58712/attachment-0003.html>
More information about the Chat
mailing list