tags:

views:

112

answers:

2

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
=> []
+8  A: 

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.

Gareth
bottom line, ancestors and superclass are not to be confused
Sam Saffron
+5  A: 

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

ennuikiller