Somebody could expect that “puts my_var” should raise “NameError:
undefined local variable or method `my_var’” in both cases.
So I can understand that in case 1) the Ruby interpreter knows about
my_var variable since it reads it before checking the “if”. But in
case 2) It seems that my_var variable is known when the script/file is
loaded since the interpreter will never run into the “if false”
statement.
On Mon, Sep 10, 2012 at 4:57 PM, Iaki Baz C. [email protected]
wrote:
undefined local variable or method `my_var’" in both cases.
The issue with expectations is that sometimes they aren’t met.
So I can understand that in case 1) the Ruby interpreter knows about
my_var variable since it reads it before checking the “if”. But in
case 2) It seems that my_var variable is known when the script/file is
loaded since the interpreter will never run into the “if false”
statement.
Am I right? Thanks a lot.
Mostly. The variable is known from the lexical position in the code
where it is defined - even if that code is not executed - as you found
out:
That’s also the reason why these behave differently although they seem
to do the same:
$ ruby -e ‘if x.nil?; x=123; end; p x’
-e:1:in <main>': undefined local variable or method x’ for
main:Object (NameError)
$ ruby -e ‘x=123 if x.nil?; p x’
123
Local variables are determined purely by the existence of an assignment
expression in the current scope. It doesn’t matter if the assignment is
ever executed.
Also note that there’s no difference between “if … end” and “… if
…” with regard to the order of evaluation. In both cases the condition
is evaluated first, and if it evaluates to true, the body is executed.