BasicObject is the parent class of all classes in Ruby. In particular, BasicObject is the parent class of class Object, which is itself the default parent class of every Ruby class:
class Foo; end Foo.superclass # => Object Object.superclass # => BasicObject
BasicObject is the only class that has no parent:
BasicObject.superclass # => nil
Class BasicObject can be used to create an object hierarchy (e.g., class Delegator) that is independent of Ruby’s object hierarchy. Such objects:
-
Do not have namespace “pollution” from the many methods provided in class
Objectand its included moduleKernel. -
Do not have definitions of common classes, and so references to such common classes must be fully qualified (
::String, notString).
A variety of strategies can be used to provide useful portions of the Standard Library in subclasses of BasicObject:
-
The immediate subclass could
include Kernel, which would define methods such asputs,exit, etc. -
A custom Kernel-like module could be created and included.
-
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) end end
What’s Here
These are the methods defined for BasicObject:
-
::new: Returns a new BasicObject instance. -
!: Returns the boolean negation ofself:trueorfalse. -
!=: Returns whetherselfand the given object are not equal. -
==: Returns whetherselfand the given object are equivalent. -
__id__: Returns the integer object identifier forself. -
__send__: Calls the method identified by the given symbol. -
equal?: Returns whetherselfand the given object are the same object. -
instance_eval: Evaluates the given string or block in the context ofself. -
instance_exec: Executes the given block in the context ofself, passing the given arguments. -
method_missing: Called whenselfis called with a method it does not define. -
singleton_method_added: Called when a singleton method is added toself. -
singleton_method_removed: Called when a singleton method is removed fromself. -
singleton_method_undefined: Called when a singleton method is undefined inself.
#define rb_obj_initialize rb_obj_dummy0
Returns a new BasicObject.
VALUE
rb_obj_equal(VALUE obj1, VALUE obj2)
{
return RBOOL(obj1 == obj2);
}
Returns whether self and other are the same object:
object = Object.new object == object # => true object == Object.new # => false
Here in class Object, ==, equal?, and eql? are the same method. A subclass may override == to provide class-specific meaning.
Unlike ==, the equal? method should never be overridden by subclasses as it is used to determine object identity (that is, a.equal?(b) if and only if a is the same object as b):
obj = "a" other = obj.dup obj == other #=> true obj.equal? other #=> false obj.equal? obj #=> true
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 any pair of objects where eql? returns true, the hash value of both objects must be equal. So any subclass that overrides eql? should also override hash appropriately.
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: