From: "Joseph E. Savard" Date: 2010-08-18T09:21:11+09:00 Subject: Re: question on if / defined?() Shy of looking at the code: The following appears that when using the ternary operator it sheds a little light on the state of x.state, in our case true or false. In the first case x is assigned to true or false. Its forced into a state. Now we follow up with y returning false, also. In either case, we set the receiver (lvalue) to a known ! irb ruby-1.9.2-preview3 > x = !defined?(x) ? true : false => false ruby-1.9.2-preview3 > x => false ruby-1.9.2-preview3 > y = !defined?(x) ? true : false => false ruby-1.9.2-preview3 > y => false When we go into the assignment x=true the if statement will execute and upon its success will store true into the x variable. I am suggesting this x variable must be instantiated as the line is digested by the compiler and therefore is instantiated as x with no assignment. Hence, we have one state true and the opposite is no state or nil The result this statement finds it if false nil (no state) is reported (returned) as the if has failed to fire and invoke the intended statement... No statement, nil. ruby-1.9.2-preview3 > x = true if !defined?(x) => nil However, x is untouched and is still set to false, below. ruby-1.9.2-preview3 > x => false ruby-1.9.2-preview3 > l = true if !defined?(x) => nil ruby-1.9.2-preview3 > l => false ruby-1.9.2-preview3 > The result below seems to follow this thinking: If is fired and assignment is invoked and x than is assigned true. ruby-1.9.2-preview3 > x = true if defined?(x) => true ruby-1.9.2-preview3 > x => true ruby-1.9.2-preview3 > So, the statement on the left of an if Statement must be parsed and instantiated for the compiler to have something to invoke on the if statement being successful. I would think this is a compiler decision of optimistic logic. The reverse of this cold be the firing than instantiates the statement to be invoked which would be pessimistic and seems to be open up a rabbit hole that may pose a problematic recovery. > From: Colin Bartlett > Reply-To: > Date: Wed, 18 Aug 2010 06:23:15 +0900 > To: ruby-talk ML > Subject: Re: question on if / defined?() > > On Tue, Aug 17, 2010 at 6:30 PM, John Sikora wrote: > >> Rick Denatale wrote: >>> Because x is defined, this happens at parse time so it doesn't matter >>> if the assignment is executed or not. >> >> Is this because when the parser sees 'x = ...', Ruby automatically >> assigns a reference to the variable x? >> > > I think that's correct, but I'd welcome confirmation from those more > knowledgeable about this. > > irb > x > #=> NameError: undefined local variable or method 'x' for main:Object > x = x > #=> nil > > Sort of assigning itself by it's own bootstraps?