rubyoopinheritancemetaclasseigenclass

How does Inheritance work in Ruby?


According to Dave Thomas in his talk about the Ruby Object Model, there are no "class methods" in Ruby. There is only difference between whether the receiver of the method is a "class object" or an "instance object".

class Dave
  def InstaceMethod              ### will be stored in the current class (Dave)
    puts "Hi"
  end
  class << self                  ### Creates an eigenclass, if not created before
    def say_hello
      puts "Hello"
    end
  end
end

By default, ancestors method doesn't show the metaclass:

class Dave
  class << self
    def metaclass                ### A way to show the hidden eigenclass
      class << self; self; end
    end
  end
end

p Dave.ancestors
# => [Dave, Object, Kernel, BasicObject]
p Dave.metaclass.ancestors
# => [Class, Module, Object, Kernel, BasicObject]

However, I assume the real one would be something like:

# => [<eigenclass>, Class, Module, Object, Kernel, BasicObject]

p Dave.class.instance_method(false)
# => [:allocate, :new, :superclass]
p Dave.metaclass.instance_method(false)
# => [:say_hello, :metaclass]

Now the inheritence.

class B < Dave
end

p B.say_hello
# => "Hello"
p B.ancestors
# => [B, Dave, Object, Kernel, BasicObject]
p B.class.instance_methods(false)
# => [:allocate, :new, :superclass]

The following would create a new eigenclass for B:

p B.metaclass.ancestors
# => [Class, Module, Object, Kernel, BasicObject]
p B.metaclass.instance_method(false)
# => []
  1. How would the B.ancestors and B.metaclass.ancestors look like when the eigenclasses are also included? The method say_hello is stored in an eigenclass, (which I assume B.class inherits from) but where is that?

  2. Since there are two ancestor chains (B.ancestors and B.class.ancestors or B.metaclass.ancestors), how does the inheritance actually take place?


Solution

  • Eigenclass is a sneaky hidden one. You have successfully revealed it by opening class. But it does not exist in the ancestors of normal class. And since it is hidden, you cannot see it by sending ancestors method to a eigenclass itself. The inheritance tree is like the following:

    B ---S-->  Dave   ---S---> Object  ---S---> BasicObject
    |            |               |                  |
    E            E               E                  E
    |            |               |                  |
    #B --S--> #Dave   ---S---> #Object ---S---> #BasicObject --S---> Class,,Object,BasicObject
    

    S stands for superclass, while E for eigenclass.