From: Robert Klemme Date: 2005-03-16T00:26:16+09:00 Subject: Re: eval/binding question "Stefan Kaes" schrieb im Newsbeitrag news:4236E882.3050602@gmx.net... > I tried to create local variables from a name=>value hash passed as a > parameter, but I can't get it to work. The following test program shows > what is happening: > > def test1 > eval "x=25" > eval 'print "x=#{x}\n"' > end > > test1 > > def test2 > eval "x=25" > print "x=#{x}\n" > end > > test2 > > produces: > > x=25 > test.rb:189:in `test2': undefined local variable or method `x' for > main:Object ( NameError) > from test.rb:192 > > It seems that changes to local variables are not reflected back to the > executing environment. > > Does anyone out there know how to solve this problem? You can't. Local variables are determined during parse phase and you cannot access local variables that you create via eval other than via eval. Consider 16:11:59 [robert.klemme]: ruby -e 'eval "x=10";puts x' -e:1: undefined local variable or method `x' for main:Object (NameError) 16:13:51 [robert.klemme]: ruby -e 'x=nil;eval "x=10";puts x' 10 16:13:57 [robert.klemme]: ruby -e 'eval "x=10";eval "puts x"' 10 IOW, you can manipulate predefined locals but not those that spring into existence during an eval. Kind regards robert