Lloyd Zusman
2/20/2005 11:32:00 AM
Pit Capitain <pit@capitain.de> writes:
> [ ... ]
>
> In addition to what Joel already said, note that
>
> mod = Module.new {
> load('modtest')
> }
>
> is the same as
>
> mod = Module.new
> load('modtest')
>
> Kernel#load doesn't load the code in the context where it is called, but
> in the context of the toplevel object (main) or in the context of an
> anonymous module, if you set the second parameter to true. You can test
> this if you put a "p self" into the file modtest.rb.
Yes, I see. Thanks.
I think that it would be nice if in a future ruby release, the
two-argument form of load() would return the anonymous module. Then,
with the following modtest file ...
X = 1
Y = 2
... I could then do this:
mod = load(modtest, true)
p mod.constants
... which should print this: ["X", "Y"]
The 'mod' return value would still be considered 'true', which is what
that call returns today if the load succeeds. In that case, we could at
least access that anonymous module if we wish.
Or here's another possibility. The second argument would get returned
to the caller, and if that argument was a module, the load would take
place within its context; otherwise, the load would just take place in
an anonymous module like today:
mod0 = Module.new
mod1 = load(modtest, mod0)
p mod0
p mod1
In this case, the load could take place within the mod0 module, which
would be returned; therefore, mod0 and mod1 would be equivalent after
this call. This would then print ["X", "Y"] twice.
Thoughts?
--
Lloyd Zusman
ljz@asfast.com
God bless you.