From: Jamal Soueidan Date: 2007-04-05T06:56:46+09:00 Subject: Re: nil? and non-existent objects 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. You could also fix it this way :) ---------------------------------------------- def x @x end def x=(val) @x = val end puts x=1 if x.nil? ---------------------------------------------- This would output: 1 -- Posted via http://www.ruby-forum.com/.