Hi all,
There have been a few threads recently both here and on the Rails list where the problem of namespace collisions has come up. It seems to my untutored eye that they've come about simply because the original authors didn't apply their namespacing universally. From memory, an example is htmltools and ActionPack both defining an HTML::Tag class.
A possible solution sprung to mind: how does the following syntax look?
require 'foo', :into => 'Bar'
require 'qux', :into => 'Wibble::Spang'
What that would do is require the named file such that all its code was executed in the context of the named module. My meta-fu isn't quite up to giving even a simple example, but I'd envisage the result looking something like this:
$ cat bar.rb
class Bar
end
$ cat test.rb
require 'bar', :into => 'Foo'
p Foo.constants
$ ruby test.rb
["Bar"]
With the htmltools and ActionPack example, this:
require 'htmltools', :into => 'MyHtmltoolsModule'
require 'actionpack', :into => 'MyCollisionModule'
would result in MyHtmlToolsModule::HTML::Tag and MyCollisionModule::HTML::Tag existing side-by-side.
Would this require help from the interpreter? require and load both execute the code at the top level of the namespace (unless I've missed something) and without overloading them to actually eval() the code differently, I'm not sure what's best.
Any thoughts? Has this been discussed to death before my time? Is it obviously stupid?
It's just struck me that if we're getting help from the interpreter anyway, this might be possible:
require 'foo' into Bar
which I think is quite neat. In case I haven't quite made it clear, the main advantage I can see from this is that modules can be protected from each other without the original author needing to care how their module is going to be used, and it can be retrospectively applied to existing code (give or take any meta- or cleverer-than-me-programming that makes this void, of course ) to fix exactly the sort of collision problems that are already occurring.
···
--
Alex