From: Brian Candler Date: 2007-04-05T06:26:37+09:00 Subject: Re: nil? and non-existent objects On Thu, Apr 05, 2007 at 04:25:23AM +0900, Fran?ois Montel wrote: > Why is it that the nil? method can sometimes be called on an object that > doesn't exist, and other times will return an error. Under what > circumstances can you call the nil? method on a non-existent object? > > Consider: > > >> puts x = 1 if x.nil? > => 1 > >> puts ???z is not defined??? if z.nil? > => NameError: undefined local variable or method ???z??? Maybe easier to understand if you consider this: if false x = 0 end puts x.inspect # => nil When the file is *parsed*, the assignment "x = 0" flags "x" as a local variable from that point onwards within the scope. This is true whether or not it is actually executed; it's a parse-time decision as to whether "x" is a local variable or a method call with implicit receiver. Regards, Brian.