Because of the way irb works, there is a minor incompatibility between
it and the standard Ruby interpreter. The problem lies in the
determination of local variables.
Normally, Ruby looks for an assignment statement to determine if
something is a variable---if a name hasn't been assigned to, then Ruby
assumes that name is a method call.
produces:
prog.rb:2: undefined local variable or method `a' for #<Object:0x401c2ce0> (NameError)
|
In this case, the assignment is there, but it's within a string, so
Ruby doesn't take it into account.
irb, on the other hand, executes statements as they are entered.
irb(main):001:0> eval "a = 0"
0
irb(main):002:0> a
0
In irb, the assignment was executed before the second line was
encountered, so ``a'' is correctly identified as a local variable.
If you need to match the Ruby behavior more closely, you can place
these statements within a
begin
/
end
pair.
irb(main):001:0> begin
irb(main):002:1* eval "a = 0"
irb(main):003:1> a
irb(main):004:1> end
NameError: undefined local variable or method `a'
(irb):3:in `irb_binding'