[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Kernel.load() behaviour

Andrew Walrond

4/1/2005 4:02:00 PM

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?

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....


27 Answers

Robert Klemme

4/1/2005 4:22:00 PM

0


"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

Andrew Walrond

4/1/2005 6:05:00 PM

0

On Friday 01 April 2005 17:24, Robert Klemme wrote:
>
> 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.

Thanks - I'll do a search.
>
> 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. :-)
>

Not quite; Each of the 000s of packages is in it's own file.... ;)

Andrew


Aredridel

4/1/2005 6:48:00 PM

0

>
> Not quite; Each of the 000s of packages is in it's own file.... ;)

eval "module Packages\n" << `cat *.rb` << "\nend"

;-)


Andrew Walrond

4/1/2005 9:11:00 PM

0

On Friday 01 April 2005 19:48, Aredridel wrote:
> > Not quite; Each of the 000s of packages is in it's own file.... ;)
>
> eval "module Packages\n" << `cat *.rb` << "\nend"
>
> ;-)

You are a _very_ twisted individual ;)


Robert Klemme

4/1/2005 9:21:00 PM

0


"Andrew Walrond" <andrew@walrond.org> schrieb im Newsbeitrag
news:200504011904.16688.andrew@walrond.org...
> On Friday 01 April 2005 17:24, Robert Klemme wrote:
>>
>> 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.
>
> Thanks - I'll do a search.
>>
>> 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. :-)
>>
>
> Not quite; Each of the 000s of packages is in it's own file.... ;)

Then I probably misunderstood you. Care to reveal some more details of the
design and its rationale?

Kind regards

robert

Brian Mitchell

4/1/2005 9:37:00 PM

0

On Apr 1, 2005 11:48 AM, Aredridel <aredridel@gmail.com> wrote:
> >
> > Not quite; Each of the 000s of packages is in it's own file.... ;)
>
> eval "module Packages\n" << `cat *.rb` << "\nend"
>
> ;-)
>
>

module Packages
end

Packages.module_eval( `cat glibc.rb`, 'glibc.rb`) # A little more elegant.

Brian.


Andrew Walrond

4/1/2005 9:40:00 PM

0

On Friday 01 April 2005 22:24, Robert Klemme wrote:
>
> Then I probably misunderstood you. Care to reveal some more details of the
> design and its rationale?
>

Essentially, I have lots of package files in a directory, each with a single
class derived from Package; eg class Glibc < Package

Now rather than polluting the global namespace (one of the packages is called
'File' which is an obvious clash) I just wanted to load them all into the
namespace of a module, without having to add <module Packages> ... <end>
around the class(es) in every one of the thousands of package files.

Very static and very boring ;)

Andrew



Andrew Walrond

4/1/2005 9:45:00 PM

0

On Friday 01 April 2005 22:37, Brian Mitchell wrote:
> On Apr 1, 2005 11:48 AM, Aredridel <aredridel@gmail.com> wrote:
> > > Not quite; Each of the 000s of packages is in it's own file.... ;)
> >
> > eval "module Packages\n" << `cat *.rb` << "\nend"
> >
> > ;-)
>
> module Packages
> end
>
> Packages.module_eval( `cat glibc.rb`, 'glibc.rb`) # A little more elegant.
>

Almost perfect, but this avoids the external 'cat' dependency:

Packages.module_eval( IO.readlines('glibc.rb').join, 'glibc.rb')

I learned some more ruby today. Thanks all!

Andrew


Brian Mitchell

4/1/2005 10:05:00 PM

0

On Apr 1, 2005 2:44 PM, Andrew Walrond <andrew@walrond.org> wrote:
> On Friday 01 April 2005 22:37, Brian Mitchell wrote:
> > On Apr 1, 2005 11:48 AM, Aredridel <aredridel@gmail.com> wrote:
> > > > Not quite; Each of the 000s of packages is in it's own file.... ;)
> > >
> > > eval "module Packages\n" << `cat *.rb` << "\nend"
> > >
> > > ;-)
> >
> > module Packages
> > end
> >
> > Packages.module_eval( `cat glibc.rb`, 'glibc.rb`) # A little more elegant.
> >
>
> Almost perfect, but this avoids the external 'cat' dependency:
>
> Packages.module_eval( IO.readlines('glibc.rb').join, 'glibc.rb')
>
> I learned some more ruby today. Thanks all!

One more idea that lets one use load and allows anonymous modules:

glibc.rb:
class Glibc
end

some_file.rb:
module Packages
end

class Package
def self.inherited(sub)
Packages.const_set(sub.name.split("::").last, sub)
end
end

load 'glibc.rb', true

# Packages.constants == ["Glibc"]

I also have an idea on how to do this with load my appending the
anonymous module BUT I couldn't get Module.nesting to work and it
seems ruby take some shortcuts in creation of the anon Module. If I
can fix this then we'll all be able to load into specific modules
which would be nice.

Brian.


ES

4/1/2005 10:50:00 PM

0


Con fecha 1/4/2005, "Brian Mitchell" <binary42@gmail.com> escribió:

>On Apr 1, 2005 2:44 PM, Andrew Walrond <andrew@walrond.org> wrote:
>> On Friday 01 April 2005 22:37, Brian Mitchell wrote:
>> > On Apr 1, 2005 11:48 AM, Aredridel <aredridel@gmail.com> wrote:
>> > > > Not quite; Each of the 000s of packages is in it's own file.... ;)
>> > >
>> > > eval "module Packages\n" << `cat *.rb` << "\nend"
>> > >
>> > > ;-)
>> >
>> > module Packages
>> > end
>> >
>> > Packages.module_eval( `cat glibc.rb`, 'glibc.rb`) # A little more elegant.
>> >
>>
>> Almost perfect, but this avoids the external 'cat' dependency:
>>
>> Packages.module_eval( IO.readlines('glibc.rb').join, 'glibc.rb')
>>
>> I learned some more ruby today. Thanks all!
>
>One more idea that lets one use load and allows anonymous modules:
>
>glibc.rb:
>class Glibc
>end
>
>some_file.rb:
>module Packages
>end
>
>class Package
> def self.inherited(sub)
> Packages.const_set(sub.name.split("::").last, sub)
> end
>end
>
>load 'glibc.rb', true
>
># Packages.constants == ["Glibc"]

This will still pollute the namespace, though, at
the initial loading. That seemed to be Andrew's
concern.

>I also have an idea on how to do this with load my appending the
>anonymous module BUT I couldn't get Module.nesting to work and it
>seems ruby take some shortcuts in creation of the anon Module. If I
>can fix this then we'll all be able to load into specific modules
>which would be nice.

I submitted an RCR about this type of load behaviour (specifically
to enable the client programmer define a namespace for an imported
module rather than having the library author do it).

While the solutions here are likely suitable for this particular
situation, the more general problem is trying to load a file like
this:

class String
def my_string_extension()
# ...
end
end

module MyModule
class MyClass
# ...
end
end

The presumed extension of the standard String class will instead
create a new String class in the enveloping module.

>Brian.

E