BasicObject

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.

To avoid polluting BasicObject for other
users an appropriately named subclass of BasicObject should be created instead of
directly modifying BasicObject:

classMyObjectSystem<BasicObjectend

BasicObject does not include Kernel (for methods like puts) and BasicObject is outside of the namespace of the
standard library so common classes will not be found without a using a full
class path.

A variety of strategies can be used to provide useful portions of the
standard library to subclasses of BasicObject. A subclass could include
Kernel to obtain puts, exit, etc. A
custom Kernel-like module could be created and included or delegation can
be used via method_missing:

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 ::File or
::Enumerator. Like method_missing,
const_missing can be used to delegate constant lookup to
Object:

The eql? method returns true if obj
and other refer to the same hash key. This is used by Hash to test members for equality. For objects of
class Object, eql? is synonymous with
==. Subclasses normally continue this tradition by aliasing
eql? to their overridden == method, but there are
exceptions. Numeric types, for example, perform type
conversion across ==, but not across eql?, so:

Invokes the method identified by symbol, passing it any arguments
specified. You can use __send__ if the name send
clashes with an existing method in obj. When the method is
identified by a string, the string is converted to a symbol.

The eql? method returns true if obj
and other refer to the same hash key. This is used by Hash to test members for equality. For objects of
class Object, eql? is synonymous with
==. Subclasses normally continue this tradition by aliasing
eql? to their overridden == method, but there are
exceptions. Numeric types, for example, perform type
conversion across ==, but not across eql?, so:

Evaluates a string containing Ruby source code, or the given block, within
the context of the receiver (obj). In order to set the context,
the variable self is set to obj while the code is
executing, giving the code access to obj’s instance variables. In
the version of instance_eval that takes a String,
the optional second and third parameters supply a filename and starting
line number that are used when reporting compilation errors.

Executes the given block within the context of the receiver (obj).
In order to set the context, the variable self is set to
obj while the code is executing, giving the code access to
obj’s instance variables. Arguments are passed as block
parameters.

Private Instance Methods

method_missing(symbol [, *args] ) → resultclick to toggle source

Invoked by Ruby when obj is sent a message it cannot handle.
symbol is the symbol for the method called, and args are
any arguments that were passed to it. By default, the interpreter raises an
error when this method is called. However, it is possible to override the
method to provide more dynamic behavior. If it is decided that a particular
method should not be handled, then super should be called, so that
ancestors can pick up the missing method. The example below creates a class
Roman, which responds to methods with names consisting of
roman numerals, returning the corresponding integer values.