From: Clemens Hintze Date: 2000-03-23T09:58:30+01:00 Subject: [ruby-talk:02101] Re: Scope violated by import via 'require'? Yukihiro Matsumoto writes: > Hi, Hi too, > > In message "[ruby-talk:02084] Scope violated by import via 'require'?" > on 00/03/22, Clemens Hintze writes: > > |But if I put the definition of the inner module 'Bar' in a file on its > |own called e.g. 'mBar.rb' and 'require' this file during module > |definition of 'Foo', 'Bar' will be known both within 'Foo' and outside > |of it: > > Well, it's an expected behavior (at least I as expect it ;-) > > require()/load() evaluates the contents of the file at the toplevel. So does that mean it works as you intended? May I ask another question? How can I achieve the goal described above? Is there any way? What is the reason for my desire? Sometimes I use helper modules/classes to do some tasks for the 'real' module/class. Sometime the helper is even a C extension. But of course, I would not like to pollute the global namespace with the names of the helper classes. That is because I am really lazy during inventing names. Often one of my helper classes is called e.g. Parser ;-) The helper classes are large enough to put them into an own file. Furthermore I sometimes expect to rewrite them as C extension for sake of performance. Put them into another file allows me to simply replace the Ruby written extension with the C written one. No change in the calling code necessary. So let me sum up my question again: Is there any way that allows me to put my module/class into an own file, and require it *into* another module/class's namespace without polluting the toplevel namespace? ... > module Bar is defined at the toplevel. And in 1.5.x, `::' notation > accesses the constants defined in superclasses too, which makes > Foo::Bar valid. That means, it is not in the module Foo, but will be found above in Object, yes? Tricky, tricky! ;-) > > matz. > \cle -- Clemens Hintze mailto: c.hintze@gmx.net