What's the difference between these classes? What's the difference between their purposes?
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.
Objects are instances of the class. You will now learn how to create objects of a class in Ruby. You can create objects in Ruby by using the method new of the class. The method new is a unique type of method, which is predefined in the Ruby library. The new method belongs to the class methods.
Object is the default root of all Ruby objects. Object inherits from BasicObject which allows creating alternate object hierarchies. Methods on Object are available to all classes unless explicitly overridden. Object mixes in the Kernel module, making the built-in kernel functions globally accessible.
Practically everything in Ruby is an Object, with the exception of control structures. Whether or not under the covers a method, code block or operator is or isn't an Object, they are represented as Objects and can be thought of as such.
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 Object class Tracer instance_methods.each do |m| next if [:__id__, :__send__].include? m undef_method m end # some logic end # when you inherit BasicObject class Tracer < BasicObject # some logic end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With