Accessing Objects from Anonymous Namespace

Hi,

Is there any way to access objects (eg. constants) from within a 'module’
that was loaded with wrap=true, like

foo.rb:
X = 1

baz.rb:
load(‘foo.rb’,true)

puts X

?

Cheers,
LF

···


+++ GMX - Mail, Messaging & more http://www.gmx.net +++
Bitte lächeln! Fotogalerie online mit GMX ohne eigene Homepage!

lunfis@gmx.de schrieb im Newsbeitrag
news:31611.1056428138@www25.gmx.net

Hi,

Is there any way to access objects (eg. constants) from within a
‘module’
that was loaded with wrap=true, like

foo.rb:
X = 1

baz.rb:
load(‘foo.rb’,true)

puts X

?

I don’t think, you can access them from the outside, because the purpose
of wrap=true is exactly to hide them:

“Any local variables in the loaded file will not be propagated to the
loading environment.”

If you want to expose them you’ll better put them into a module and load
with wrap = false. After all, why do you hide them if you want to expose
them?

Regards

robert

lunfis@gmx.de wrote:

Is there any way to access objects (eg. constants) from within a ‘module’
that was loaded with wrap=true, like

foo.rb:
X = 1

baz.rb:
load(‘foo.rb’,true)

puts X

AFAIK, only through nefarious programming:

def collect_modules
mods =
ObjectSpace.each_object(Module){|m| mods << m }
mods
end
before = collect_modules
load(‘foo.rb’,true)
after = collect_modules
m = (after - before)[0]
puts m::X

Very thread unsafe btw. There probably exists some neat hooks, but I
can’t find any right now.

···


([ Kent Dahl ]/)_ ~ [ http://www.pvv.org/~kentda/ ]/~
))_student_/(( _d L b_/ (pre-) Master of Science in Technology )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)

Hi,

···

At Tue, 24 Jun 2003 13:15:43 +0900, lunfis@gmx.de wrote:

Is there any way to access objects (eg. constants) from within a ‘module’
that was loaded with wrap=true, like

foo.rb:
X = 1

Not impossible, but strongly unrecommended.

::Object.class_eval{const_set(:X, 1)}


Nobu Nakada

Not impossible, but strongly unrecommended.

  ::Object.class_eval{const_set(:X, 1)}

Why not give the possibility ?

  load(file, true) # anonymous module

  module M end
  load(file, M) # loaded under M

Guy Decoux