BasicObject is the parent class of all classes in Ruby. It's an explicit blank class.
BasicObject can be used for creating object hierarchies independent of Ruby's object hierarchy, proxy objects like the Delegator class, or other uses where namespace pollution from Ruby's methods and classes must be avoided.
class MyObjectSystem < BasicObject end
A variety of strategies can be used to provide useful portions of the
standard library to subclasses of BasicObject. A subclass could
Kernel to obtain
exit, etc. A
custom Kernel-like module could be created and included or delegation can
be used via method_missing:
class MyObjectSystem < BasicObject DELEGATE = [:puts, :p] def method_missing(name, *args, &block) return super unless DELEGATE.include? name ::Kernel.send(name, *args, &block) end def respond_to_missing?(name, include_private = false) DELEGATE.include?(name) or super end end
Access to classes and modules from the Ruby standard library can be
obtained in a BasicObject subclass by
referencing the desired constant from the root like
::Enumerator. Like method_missing,
const_missing can be used to delegate constant lookup to
class MyObjectSystem < BasicObject def self.const_missing(name) ::Object.const_get(name) end end