SYNOPSIS
The purpose of namespace is probably not just to define a scope for classes, I think there are cases when it could be used as placeholder of something shared among these classes. The generic code accessing whatever is shared could use #namespaces method, which just returns Array of modules enclosing the callee. Here is an example of shared constant XML_NAMESPACE and generic code in Atom::Base class:
require 'rubygems'
require 'namespaces'
module Atom
XML_NAMESPACE = 'http://www.w3.org/2005/Atom'
class Base
extend Namespaces
def self.inherited(klass) # generic code:
puts klass.namespaces[-1]::XML_NAMESPACE
end
end
class Entry < Base; end
class Feed < Base; end
end
module GData
XML_NAMESPACE = 'http://schemas.google.com/g/2005'
class Entry < Atom::Entry; end
class Feed < Atom::Feed; end
end
module GCalendar
XML_NAMESPACE = "http://schemas.google.com/gCal/2005"
class Entry < GData::Entry; end
class Feed < GData::Feed; end
end
The output is:
http://www.w3.org/2005/Atom
http://www.w3.org/2005/Atom
http://schemas.google.com/g/2005
http://schemas.google.com/g/2005
http://schemas.google.com/gCal/2005
http://schemas.google.com/gCal/2005