Tom Cloyd wrote: > Trying this for the first time, and, as usual, not quite getting it. > > Everything I can find on kernal::binding tells me that I just call it > and will get back a binding object which can be passed in a call to > eval. But...it doesn't work for me. > > In my main, I have a module required which contains a method I'm trying > to call. For reasons irrelevant to this current problem, I'm using an > eval of a string to do it, and want the method to execute in the > environment of the main program. So... > > eval( "moduleName.methodName", binding) > > Causes *crash* due to by attempt to access a variable which exists in > the calling environment but not in the module. In spite of passing the > binding, the method knows nothing of the variable. It's not working. > > All examples I can find just do something like.. > > def meth_x > y = 1 > return binding > end > > Then 'y' is accessed using the binding. Well, if one can return a > binding to initialize a variable - > > z = meth_x > eval( "y", z ) # => "1" > > Then why can't I simply pass 'binding' in an eval call FROM the calling > environment? You've got something like this, right? module M def M.meth p x end end x = 1 eval "M.meth", binding # Undefined variable x The binding that you pass into eval here only affects variables that are unbound in the string. In this case there are none. The x in M.meth is a bound variable in the scope of the method. Compare: module M def M.meth arg p arg end end x = 1 eval "M.meth(x)", binding # ==> 1 Also: x = 1 eval %{ p x # ==> 1 def foo p defined?(x) end foo # ==> nil }, binding Of course, it's quite possible I didn't understand the question... What are you trying to do? -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407