What's the difference between Object and BasicObject in Ruby? What's the difference between Object and BasicObject in Ruby? ruby ruby

What's the difference between Object and BasicObject in Ruby?


BasicObject was introduced in Ruby 1.9 and it is a parent of Object (thus BasicObject is the parent class of all classes in Ruby).

BasicObject has almost no methods on itself:

::new#!#!=#==#__id__#__send__#equal?#instance_eval#instance_exec

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.

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.


Object mixes in the Kernel module, making the built-in kernel functions globally accessible. Although the instance methods of Object are defined by the Kernel module...

You can use BasicObject as a parent of your object in case if you don't need methods of Object and you would undefine them otherwise:

# when you inherit Objectclass Tracer  instance_methods.each do |m|    next if [:__id__, :__send__].include? m    undef_method m  end  # some logicend# when you inherit BasicObjectclass Tracer < BasicObject  # some logicend