"Orion Hunter" <orion2480 / hotmail.com> schrieb im Newsbeitrag
news:BAY2-F7808xWI0AT2Jv00000db0 / hotmail.com...
> I am fiddling around with the eval() function, and loading information
at
> runtime.  The following works as expected:
>
> $> ruby -e 'str = "def foo; 10; end"; eval(str); puts foo+1'
> 11
>
> However, why does this not work?
>
> $>ruby -e 'str = "a = 2"; eval(str); puts a'
> -e:1: undefined local variable or method `a' for #<Object:0x124b00>
> (NameError)

try this

$>ruby -e 'str = "a = 2"; eval(str, binding); puts a'

You need to give the current context for locals, otherwise eval creates
its own scope.

    robert