I am getting NameError: undefined local variable or method
with ruby 2.1.2
As observed in this question, expressions like:
bar if bar = true
raises an undefined local variable error (provided that bar
is not defined prior) because bar
is read by the parser before it is assigned. And I believe that there used to be no difference with that with this expression:
bar if bar = false
The difference between the two is whether the main body is evaluated or not, but that should not matter if encountering an undefined local variable immediately raises an error before evaluating the condition.
But when I run the second code on Ruby 2.1.2, it does not raise an error. Has it been like that from before? If so, then what was the parsing discussion all about? If not, has Ruby specification changed? Is there any reference to that? What did it do in 1.8.7, 1.9.3, etc. ?
Yes it changed in ruby 2.1.2
In
1.8.7
,1.9.3
,2.0.0
and even2.1.1
I get 2 warnings and no errors:whereas in the
2.1.2
version you mention I get 2 warnings and 1NameError
error.This is on my Ubuntu 14