From: Saynatkari Date: 2005-04-02T07:59:59+09:00 Subject: Re: Kernel.load() behaviour Con fecha 1/4/2005, "Saynatkari" escribi�: > >Con fecha 1/4/2005, "Brian Mitchell" escribi�: > >>On Apr 1, 2005 2:44 PM, Andrew Walrond wrote: >>> On Friday 01 April 2005 22:37, Brian Mitchell wrote: >>> > On Apr 1, 2005 11:48 AM, Aredridel 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. Meh. Obviously ignore that. >>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