"Namespace on Read" for Ruby, FAQ
gist.github.com
gist.github.com
# your_module.rb
module YourModule
end
# my_module.rb
require 'your_module'
module MyModule
end
# example.rb
namespace1 = NameSpace.new
namespace1.require('my_module') #=> true
namespace1::MyModule #=> #<Module:0x00000001027ea650>::MyModule (or # <NameSpace:0x00...>::MyModule ?)
namespace1::YourModule # similar to the above
MyModule # NameError
YourModule # NameErrorMost people are talking about how this would allow you to require multiple versions of the same library (each in its own NameSpace), but I think that would be a bad use of it. I don't remember the last time I had a problem with conflicting libraries in Ruby, and I think the last time I did I just forked the offending library.
A more interesting use case is to have a framework like Rails use it to define namespaces for files without those files having to constantly refer to the name of the namespace they're in. That's something that's bugged me anyway.
module SomeGrouping
class Some::Nested
end
class Whatever
def foo
Some::Nested.new
end
end
end
Sorry typing code on phone is a nightmareCurious to see how Namespace will work in practice with all the metaprogramming that's so common in popular Ruby libraries. I can also see this enabling applications to use multiple versions of the same library or module, for better or worse.