https://github.com/zimbatm/namespace.rb
Experiment: Bringing namespaces to Ruby
https://github.com/zimbatm/namespace.rb
Last synced: over 1 year ago
JSON representation
Experiment: Bringing namespaces to Ruby
- Host: GitHub
- URL: https://github.com/zimbatm/namespace.rb
- Owner: zimbatm
- Created: 2010-11-20T22:56:12.000Z (over 15 years ago)
- Default Branch: master
- Last Pushed: 2023-12-15T11:42:50.000Z (over 2 years ago)
- Last Synced: 2025-02-27T05:23:16.532Z (over 1 year ago)
- Language: Ruby
- Homepage:
- Size: 9.77 KB
- Stars: 5
- Watchers: 4
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.rdoc
Awesome Lists containing this project
README
= Namespaces for ruby
It is an experiment to bring namespaces to ruby. It is not quite clear yet
why this would be useful, but it can be interesting nonetheless.
This system is inspired by the CommonJS and Python module systems, adapted
for the ruby particularities.
By loading the .rb files into a temporary module, it is possible to avoid
polluting the global namespace and select what to import in the current module.
Features:
* Compatible with ruby 1.8.7 and 1.9.2 (and others?)
* Possible to avoid namespace collision on the module level
Unfeatures:
* uses eval and other nasty ruby hacks
* classes and namespaces don't mix well because `class X; <>; end`
is a new context that doesn't inherit from it's parent.
== Usage
See: +Namespace.open+
m = Namespace.open("hello::world")
m.hello
#=> "hello world"
In the context of "hello/world.rb" Namespace#import is made available as
a shortcut.
X = import "another::module"
def hello
X::Joiner.new("hello", "world").to_s
end
Classes and modules are also exported:
class Joiner
def initialize(*a)
@args = a
end
def to_s
@args.join(' ')
end
end
== TODO
* Handle circular dependencies ?
* Avoid eval (but I don't see how)
* I just learned that Kernel#load(path, wrap=true) will load the file in
an anonymous module.
== Tricks
A module can emulate the main object behavior by extending itself. In that way,
it makes it's functions directly available to consumption.
module X
extend self
def test
"x"
end
test #=> "x"
end
TODO
== Bikeshed
* Should ruby "namespaced" modules have their own $LOAD_PATH and file extensions ?
== Licence
Public domain
Cheers, Jonas