Why should the above statement be valid, although a was not predefined
prior to
the assignment?
Ruby version is ruby 1.9.3p125
saji
Saji N Hameed,
ARC-ENV, Center for Advanced Information Science and Technology,
University of Aizu, Tsuruga, Ikki-machi,
Aizuwakamatsu-shi, Fukushima 965-8580,
Japan
Why should the above statement be valid, although a was not predefined prior
to the assignment?
A local variable is valid from the first point in the surrounding
scope where it is assigned. Since the assignment (left hand side of
the expression) comes before the evaluation (right hand side) it is
completely legal. That rule has also other consequences:
$ ruby -e ‘puts a if a=1’
-e:1: warning: found = in conditional, should be ==
-e:1:in <main>': undefined local variable or methoda’ for
main:Object (NameError)
Execution order is different than textual order but that’s the way it
is.
Robert, Thanks a lot for the explanation - it makes sense now !
saji
Saji N Hameed,
ARC-ENV, Center for Advanced Information Science and Technology,
University of Aizu, Tsuruga, Ikki-machi,
Aizuwakamatsu-shi, Fukushima 965-8580,
Japan