From: Emmanuel Oga Date: 2007-04-05T04:46:12+09:00 Subject: Re: nil? and non-existent objects I think is because of the ruby's analisis on the scope. That is, in: puts x = 1 if x.nil? ruby sees x in the scope even if it is never accesed nor asigned, but in puts ‘z is not defined’ if z.nil? z is never in scope, because it is present only inside a string, it does not appear anywhere before the call of the method "nil?" you can do: { z } if z.nil? => returns nothing of nothings (not nil nor false) ruby don't complain because z is seen in the scope Also works: "HELLO #{an_unique_var_name}" if an_unique_var_name.nil? ------------------------------------------------------------------------------- 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’ > > Someone mentioned that = has higher precedence than if, so it could be > that in the first statement x is assigned 1 before the conditional > statement is run. However, x.nil? would then always return false. -- Posted via http://www.ruby-forum.com/.