Kernel#load and locally scoped variables

Is there any way for a Kernel#load(file) code block to gain access to
the variables that are in scope at the point at which that file is
loaded? Anyone have a code example that they could share? Or an
explanation as to why it doesn't work?

Thanks

John

John wrote:

Is there any way for a Kernel#load(file) code block to gain access to
the variables that are in scope at the point at which that file is
loaded? Anyone have a code example that they could share? Or an
explanation as to why it doesn't work?

Sure. Use read and eval instead of load:

---- a.rb ----

x = nil # the variable must "exist" before the eval
load "b.rb"
p x # ==> nil
eval File.read("b.rb")
p x # ==> 1

---- b.rb ----

x = 1

ยทยทยท

----

This is a little "evil", though. My preferred alternative is to wrap the loaded file in a module and use that module's scope to access methods and constants (but local vars stay local), as in http://ruby-talk.org/100429\.