From: Tim Hunter Date: 2008-03-30T08:22:16+09:00 Subject: Re: Extrange behaviour using "if XXX ..." and "XXX if ..." I単aki Baz Castillo wrote: > Hi, note the difference in those both cases: > > a) > if a="QWEQWE" : a end > => "QWEQWE" > > b) > a if a="QWEQWE" > NameError: undefined local variable or method `a' for main:Object > > > I know that the behaviour in a) is no recommended and maybe deprecated in a > future, but why it works in a) and not in b) ? A method call without parentheses and a plain variable look the same, so Ruby tries to determine between the two by looking for an assignment as it parses your script. If it sees you assigning a value to "a", then it knows that "a" is a variable, otherwise it assumes that "a" is a method call. Parsing occurs from the top down and from left to right. In case (a) the assignment to "a" occurs before any other reference, so Ruby knows it's a variable. 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. -- RMagick: http://rmagick.rubyforge.org/ RMagick 2: http://rmagick.rubyforge.org/rmagick2.html