Robert Klemme
4/1/2005 4:22:00 PM
"Andrew Walrond" <andrew@walrond.org> schrieb im Newsbeitrag
news:200504011701.25701.andrew@walrond.org...
> I want to wrap a pile of classes, which are defined in their own files,
inside
> a wrapper module. Basically, I want this to work:
>
> glibc.rb:
> class Glibc
> end
>
> test.rb:
> module Packages
> load 'glibc.rb'
> end
>
> Packages::Glibc.new
>
>
> But it doesn't do what I expected. Glibc gets added to the global
namespace,
> rather than the Packages namespace.
>
> I.e. Glibc.new works; Packages::Glibc.new doesn't
>
> Now, I see Kernel.load() is documented as
>
> Kernel.load(filename,wrap=false)
>
> If wrap==true, then "the loaded script will be executed under an
anonymous
> module, protecting the calling program's global namespace"
>
> I guess I need the equivalent of
> Kernel.load(filename,wrapper_module=nil)
> So I can do
> load('glibc.rb',Packages)
>
> Or is there another way?
This has come up recently. Unfortunately there's no chance of getting at
the anonymous module other than from within the loaded file. I'd wish it
was changed like this:
Kernel.load(file, mode=nil)
Where "mode" is interpreted like this:
- if it's nil or false, no wrapping takes place, return value is the
parameter
- if it's a module, that is used for wrapping, return value is the module
- if it's anything else (i.e. equivalent to true) an anonymous module is
created for wrapping, return value is the anonymous module
You might find more on this in the archives.
> Suggestions appreciated
>
> Andrew Walrond
>
> PS Explicitly putting Packages:: on each class definition is possible,
but
> there are 000s of packages so I was looking for a better way....
You don't need that: simply wrap the file you want to load with
module Packages
# all 000s class definitions here
end
As easy as that. :-)
Kind regards
robert