In Ruby (1.8.X)
Why does Object both inherit off Kernel and include it? Wouldn't just inheriting be enough?
irb(main):006:0> Object.ancestors => [Object, Kernel] irb(main):005:0> Object.included_modules => [Kernel] irb(main):011:0> Object.superclass => nil
Note in Ruby 1.9 the situation is similar (but a bit cleaner):
irb(main):001:0> Object.ancestors => [Object, Kernel, BasicObject] irb(main):002:0> Object.included_modules => [Kernel] irb(main):011:0> Object.superclass => BasicObject irb(main):011:0> BasicObject.superclass => nil irb(main):011:0> BasicObject.included_modules => []
The Object and Kernel classes are the root of all Ruby class and object functionality. Object is the base class that all Ruby classes inherit from. The Kernel module is included by the Object class and contains methods that can be used in all classes.
Sub class: This is the class that is derived from the parent class. It is also known as a subclass or derived class or child class. You can also add its own objects, methods, along with the base class methods and objects, etc. Note: By default, every class in Ruby has a Super class.
Nesting class definitions in Ruby is a matter of preference, but it serves a purpose in the sense that it more strongly enforces a contract between the two classes and in doing so conveys more information about them and their uses.
[extend] - will add the extended class to the ancestors of the extending classes' singleton class. Other words extend mix the module's functionality into class and makes these methods available to the class itself. [super] - is used for overridden method call by the overriding method.
Object
does not inherit from Kernel
, it is the final superclass (in Ruby 1.8). The result of the #ancestors
method comprises of superclasses and included modules. Specifically, in the order they are looked up for any particular call.
When you include a module in a class it becomes part of its inheritance hierarchy. Therefore the by including Kernel Object.ancestors will include (no pun intended) Kernel. In ruby versions < 1.9 Object is at the top of the hierarchy so it has no superclass
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