From: Eric Christopherson Date: 2010-02-21T13:09:55+09:00 Subject: Re: Ruby conditionals subtlety? On Thu, Feb 18, 2010 at 5:18 PM, Ryan Davis wrote: > Your latter code snippet treats "fooo" in defined? as a method call. This is because the assignment inside the conditional hasn't been parsed yet, and hasn't affected the lookup tables. > > The former doesn't have this problem because the body of the conditional is parsed first. And: On Sat, Feb 20, 2010 at 5:28 AM, Brian Candler wrote: > Maybe it's clearer like this: > >  if false >    foo = 123 >  end >  puts foo      # nil > >  puts bar      # undefined local variable or method 'bar' > > That is, for a bare word expression like 'foo' ruby has to decide > whether to parse it as a method call - as foo() or self.foo - or as a > local variable reference. > > It makes the decision based on whether there has been a previous > assignment of the form "foo = ..." parsed earlier in the code. This is > regardless of whether the code is actually executed, because we haven't > started executing any of it yet. I think I understand the gist of this -- if the parser has seen foo already, it is considered "defined" -- but how does the distinction of method vs. local variable matter in this context? As far as I can tell, defined? works the same on local variable names and method names.