<div dir="ltr">When you say foo += [], this means foo = foo + []. The first thing that Ruby sees is foo on the left hand side. Since it's an assignment, it implicitly creates a new local variable foo that's nil by default. When it comes to evaluating the right hand side, foo is a reference to this local variable, not your method.<div>

<br></div><div>I'm not 100% correct that this is exactly what's going on, so please correct me if I'm wrong.<br><div><br></div><div>Best,</div><div>Evgeny</div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">

On Tue, Jul 1, 2014 at 10:12 AM, Kenneth Lee <span dir="ltr"><<a href="mailto:kenfodder@gmail.com" target="_blank">kenfodder@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div dir="ltr"><p>Doing some lazy instantiation and come across this oddity, maybe I'm just being dense.</p><p><font face="courier new, monospace">$ irb</font></p>
<p><font face="courier new, monospace">irb(main):001:0> def foo</font></p>
<p><font face="courier new, monospace">irb(main):002:1>   @bar ||= []</font></p>
<p><font face="courier new, monospace">irb(main):003:1> end</font></p>
<p><font face="courier new, monospace">=> :foo</font></p>
<p><font face="courier new, monospace">irb(main):004:0> foo</font></p>
<p><font face="courier new, monospace">=> []</font></p>
<p><font face="courier new, monospace">irb(main):005:0> foo += []</font></p>
<p><font face="courier new, monospace">NoMethodError: undefined method `+' for nil:NilClass</font></p>
<p><font face="courier new, monospace"><span>       </span>from (irb):5</font></p>
<p><font face="courier new, monospace"><span>       </span>from /Users/ken/.rbenv/versions/2.1.1/bin/irb:11:in `<main>'</font></p>
<p><font face="courier new, monospace">irb(main):006:0> foo</font></p>
<p><font face="courier new, monospace">=> nil</font></p></div>
<br>_______________________________________________<br>
Chat mailing list<br>
<a href="mailto:Chat@lists.lrug.org">Chat@lists.lrug.org</a><br>
<a href="http://lists.lrug.org/listinfo.cgi/chat-lrug.org" target="_blank">http://lists.lrug.org/listinfo.cgi/chat-lrug.org</a><br>
<br></blockquote></div><br></div>