# Namespaces, inverted

**URL:** <https://rubytalk.org/t/namespaces-inverted/15507>\
**Category:** ruby-talk\
**Created:** [28 December 2004 22:19 UTC](https://rubytalk.org/t/namespaces-inverted/15507 "2004-12-28T22:19:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![E\_S](https://avatars.discourse-cdn.com/v4/letter/e/3be4f8/32.png) [@E\_S](https://rubytalk.org/u/E_S)\
**Post date:** [28 December 2004 22:19 UTC](https://rubytalk.org/t/namespaces-inverted/15507/1 "2004-12-28T22:19:00Z")

</div>

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

> Additionally / Alternatively it could be useful to have a per module require  
> like this:
> 
> class Module  
> &nbsp;&nbsp;def require(\*files)  
> &nbsp;&nbsp;&nbsp;&nbsp;# puts "loading in module #{self}: #{files.inspect}"  
> &nbsp;&nbsp;&nbsp;&nbsp;@loaded ||= {}  
> &nbsp;&nbsp;&nbsp;&nbsp;files.each do |f|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unless @loaded[f]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# real load code that wraps contents of f in self  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@loaded[f] = true  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> end
> 
> Then we could do
> 
> module Foo  
> &nbsp;&nbsp;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
> 
> &nbsp;&nbsp;&nbsp;&nbsp;robert

E

> **···**
>
> > \> eero.saynatkari@kolumbus.fi wrote:

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [29 December 2004 08:56 UTC](https://rubytalk.org/t/namespaces-inverted/15507/2 "2004-12-29T08:56:46Z")

</div>

"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 🙂 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 🙂

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". 🙂 But keep in  
mind the difference between load once and load always.

Regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

> **···**
>
> > \> \> eero.saynatkari@kolumbus.fi wrote:
