Project

namespace

0.0
No release in over 3 years
Low commit activity in last 3 years
This module is an experiment to bring namespaces to ruby. Each imported file is loaded in it's own context,thus avoiding global namespace pollution
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 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; <<here>>; 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