From: "François Montel" Date: 2007-04-05T07:12:17+09:00 Subject: Re: nil? and non-existent objects Jamal Soueidan wrote: > 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 Actually, it needs no fixing, or the def statements. The explanation (thanks, Brian, et al) is that "x=1 if x.nil?" works because when the parser sees 'x=1' it flags x as a local variable and creates it, assigning it the value nil. Only then does it execute x.nil?, which returns a value since x now exists. In other words, if there is any reference to a new local variable in your code, even if that reference is not executed (as in Brian's example), the local variable is created and assigned a nil value. -- Posted via http://www.ruby-forum.com/.