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

Gabe da Silveira gabe at websaviour.com
Tue Jul 1 02:46:37 PDT 2014


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>
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 listChat at lists.lrug.orghttp://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/29bbd68f/attachment.html>


More information about the Chat mailing list