I am confused with the way Ruby keeps track of variables. For example:
case 1:
if true
a
end
will give you an error saying undefined local variable or method a
.
case 2:
if false
a
end
a
will give you same the error for the second a
, not for the first a
.
case 3:
if false
a=2
end
a #=> nil
defined? a #=> 'local-variable'
If you compare case 2 and case 3, in case 2 it ignored the error first a
. I think its because of ruby's execution path has not reached the variable a
due to false
in condition. Same thing when I do with assignment in case 3. It gives me variable a
defined but with nil
value. Can someone explain the way it works?