5

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
=> []

2 Answers 2

12

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.

Sign up to request clarification or add additional context in comments.

1 Comment

bottom line, ancestors and superclass are not to be confused
5

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.