From: Jean-Hugues ROBERT Date: 2004-04-21T22:24:39+09:00 Subject: Re: binding - how to get current script? At 21:09 21/04/2004 +0900, you wrote: >On Wed, 21 Apr 2004 12:18:18 +0200, Szymon Drejewicz wrote: > > > > >> A local variable created in eval is only visible in eval. > > > > :'-( > > > > so there is no way to do something like: > > > > eval("name = 'Alice'", MAIN_BINDING) > > puts name > > > >There is :-) >You can use Kernel#binding to get the current binding, >and pass it to eval: > >irb(main):001:0> eval("name = 'Alice'", binding()) >=> "Alice" >irb(main):002:0> name >=> "Alice" However this bombs: eval("name = 'Alice'", binding); puts name This is probably because the reference to name is compiled *before* eval() is called. Basically: the local variable is created but the compiled code does not bind to it. Try this: def test() eval( "a = 'titi'", binding) puts "Compiled: #{a}" rescue eval( 'puts "Dynamic: #{a}"') end test() Jean-Hugues ------------------------------------------------------------------------- Web: http://hdl.handle.net/1030.37/1.1 Phone: +33 (0) 4 92 27 74 17