Two similar cases (same behavior):
1)
···
---------------------
my_var = 123 if false
puts my_var
=> nil
---------------------
2)
---------------------
if false
my_var = 123
end
puts my_var
=> nil
---------------------
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.
Am I right? Thanks a lot.
BTW:
--------------------
puts "1: defined? my_var: #{(defined? my_var).inspect}"
if false
my_var = 123
end
puts "2: defined? my_var: #{(defined? my_var).inspect}"
=>
1: defined? my_var: nil
2: defined? my_var: "local-variable"
--------------------
--
Iñaki Baz Castillo
<ibc@aliax.net>