[LRUG] attr_accessor parsed as a method variable when setting with a get

Jon Leighton j at jonathanleighton.com
Wed Sep 21 03:48:18 PDT 2011


On Wed, 2011-09-21 at 10:50 +0100, James Darling wrote:
> I spent quite a while trying to isolate an issue yesterday. I've done
> so now, and worked around it, but now I'm interested in what's going
> on.

As others have said, the answer is yes, it's a consqeuence of Ruby's
parser. When Ruby sees an lvar assignment, it initializes the symbol to
nil in the symbol table. Another example of this is:

def foo
  bar = "<3" if false
  bar
end

foo # => nil

Even though 'bar = "<3"' never runs, the lvar still gets initialized in
the symbol table, hence the method returns nil.

-- 
http://jonathanleighton.com/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: This is a digitally signed message part
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20110921/597e45a2/attachment-0003.sig>


More information about the Chat mailing list