Namespaces, inverted

Lähettäjä: "Robert Klemme" <bob.news@gmx.net>
"Joel VanderWerf" <vjoel@PATH.Berkeley.EDU> schrieb im Newsbeitrag
news:41D0E5EF.1040308@path.berkeley.edu...
> [Snip]

> class String
> def as mod
> {:name => self, :module => mod}
> end
> end

I don't like the idea of the as method as it cannot be used reasonable in
other circumstances as module loading. The code really does not belong into
class String IMHO.

Agreed. It just looked better :slight_smile: I suppose a top-level method
would have done as nicely for this how-it-can-be-done-now
implementation.

We have Kernel#load [1] with a similar functionality already. Currently it
accepts an additional parameter to enforce wrapping of the file in an
anonymous module. That could be extended to accept a module as well; that
then would be the module used. Then we can do

load "foo.rb" # not wrapped
load "foo.rb" # wrapped in an anonymous module
load "foo.rb", MyExternal # wrapped in MyExternal

Good. I had to rely on a 1.6 reference knowledge and didn't
recall the second parameter. Implementing it this way would,
certainly, require a change at the core level so an RCR might
be appropriate. I can write it up, I suppose.

I'm still partial to 'import' vs. 'require', though :slight_smile:

Additionally / Alternatively it could be useful to have a per module require
like this:

class Module
  def require(*files)
    # puts "loading in module #{self}: #{files.inspect}"
    @loaded ||= {}
    files.each do |f|
      unless @loaded[f]
        # real load code that wraps contents of f in self
        @loaded[f] = true
      end
    end
  end
end

Then we could do

module Foo
  require "bar"
end

and even if we do the same later again, "bar" is only loaded once (see [2]).

Looks nice, although the functionality is somewhat
overlapping, no?

Kind regards

    robert

E

···

> eero.saynatkari@kolumbus.fi wrote:

"E S" <eero.saynatkari@kolumbus.fi> schrieb im Newsbeitrag
news:20041228221857.GQPI5992.fep32-app.kolumbus.fi@mta.imail.kolumbus.fi...

> Lähettäjä: "Robert Klemme" <bob.news@gmx.net>
> "Joel VanderWerf" <vjoel@PATH.Berkeley.EDU> schrieb im Newsbeitrag
> news:41D0E5EF.1040308@path.berkeley.edu...
> > [Snip]

> > class String
> > def as mod
> > {:name => self, :module => mod}
> > end
> > end

> I don't like the idea of the as method as it cannot be used reasonable

in

> other circumstances as module loading. The code really does not

belong into

> class String IMHO.

Agreed. It just looked better :slight_smile: I suppose a top-level method
would have done as nicely for this how-it-can-be-done-now
implementation.

> We have Kernel#load [1] with a similar functionality already.

Currently it

> accepts an additional parameter to enforce wrapping of the file in an
> anonymous module. That could be extended to accept a module as well;

that

> then would be the module used. Then we can do
>
> load "foo.rb" # not wrapped
> load "foo.rb" # wrapped in an anonymous module
> load "foo.rb", MyExternal # wrapped in MyExternal

Good. I had to rely on a 1.6 reference knowledge and didn't
recall the second parameter. Implementing it this way would,
certainly, require a change at the core level so an RCR might
be appropriate. I can write it up, I suppose.

Great!

I'm still partial to 'import' vs. 'require', though :slight_smile:

Well, I'm open there. It's just that load and require have established
semantics: "load" reads the file on every occurrence while "require" loads
it only once (plus it does some file name magice to determine whether it's
"foo.rb", or "foo.so" / "foo.dll").

> Additionally / Alternatively it could be useful to have a per module

require

> like this:
>
> class Module
> def require(*files)
> # puts "loading in module #{self}: #{files.inspect}"
> @loaded ||= {}
> files.each do |f|
> unless @loaded[f]
> # real load code that wraps contents of f in self
> @loaded[f] = true
> end
> end
> end
> end
>
> Then we could do
>
> module Foo
> require "bar"
> end
>
> and even if we do the same later again, "bar" is only loaded once (see

[2]).

Looks nice, although the functionality is somewhat
overlapping, no?

Yes, that's why I wrote "Additionally / Alternatively". :slight_smile: But keep in
mind the difference between load once and load always.

Regards

    robert

···

> > eero.saynatkari@kolumbus.fi wrote: