From: "Iñaki Baz Castillo" Date: 2008-03-30T08:41:44+09:00 Subject: Re: Extrange behaviour using "if XXX ..." and "XXX if ..." El Domingo, 30 de Marzo de 2008, Tim Hunter escribió: > In case (b), there has not yet been an assignment before the reference > to a, so Ruby assumes that "a" is a method call, yet there is no > definition for a so you get the message. b) a if a="QWEQWE" NameError: undefined local variable or method `a' for main:Object But why the first "a" is interpreted before the "if" stament? Imagine this example: defined?a => nil a="BLABLA" if 2 == 3 => nil a => nil After this code "a" will remain "nil" (or the previous value it had), so: a="BLABLA" hasn't been interpreted since the "if" stament failed. Also note this other example: defined?kk => nil kk if nil => nil Why in this last case the code doesn't return: "undefined local variable or method kk' " ? I assume the reason: "kk" is not interpreted if the condition fails. In b) the first "a" is then interpreted AFTER the condition success, so in that moment "a" has been already declared (into the condition), so it would exist and not give an error. Am I not right? -- Iñaki Baz Castillo