Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Ruby Inheritance

I've been reading the Well Grounded Rubyist and it mentions how a class inherits the instance methods of its super class so that objects of the class will be able to call those instance methods. Here's an example:

class C
  def run_instance_method
    puts "This is an instance method."
  end
  def C.run_class_method
    puts "This is a class method."
  end
end

class D < C
end

Based on what I've read, it's always been described that class D would inherit just the instance methods of class C (in which case, the C::run_class_method would not be inherited by D). However, after running the above code, I notice that:

D.run_class_method # => "This is a class method."

Here's my guess as to why this is happening and please let me know if this is the correct understanding. If there is an instance d of class D and you try to run d.run_instance_method, that object will search its method-lookup path and see if that method is defined in its singleton class, its own class, or in its superclasses. Since run_instance_method is defined in class C, no issues will occur and run_instance_method will be called. For the class object D (which is a subclass of C and Object), if D.run_class_method is called, it'll again check the method lookup path for the D class object. Again, Ruby will find it in the class object C.

Is this reasoning accurate?

like image 795
wmock Avatar asked Feb 17 '13 07:02

wmock


People also ask

How many types of inheritance are there in Ruby?

Ruby supports only single class inheritance, it does not support multiple class inheritance but it supports mixins. The mixins are designed to implement multiple inheritances in Ruby, but it only inherits the interface part.

What is meant by inheritance in Ruby on Rails?

Inheritance is when a class receives or inherits the attributes and behavior of another class. The class that is inheriting the behavior is called the subclass (or derived class) and the class it inherits from is called the superclass (or base class). Imagine several classes - Cat, Dog, Rabbit, and so on.

Why multiple inheritance is not supported in Ruby?

Multiple inheritance - This is absolutely not possible in ruby not even with modules. multilevel inheritance - This is what is possible even with the modules. Why ? Modules acts as an absolute superclasses to the class that includes them.

What is a subclass in Ruby?

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.


2 Answers

Class methods may be inherited and overridden just as instance methods can be. If your parent class defines a class method, the subclass inherits that method. That is, if your subclass does not define it's own class method, then it inherits from it's superclass.

As a recommendation: when invoking a class method with an explicit receiver, you should avoid relying on inheritance. Always invoke the class method through the class that defines it. Otherwise, it would be very difficult for someone who relies on your code to find the parent class which defines the class method.

Referring back to your original assumption: the invocation of a class method from a subclass it's possible because the class methods are instance methods of the eigenclass.

class C
   # instance methods goes here
   class << self # open the eigenclass
   # class methods go here as instance methods of the eigenclass
   end
end

In general, it's clearer to define class methods as individual singleton methods without explicitly opening the eigenclass.

For a clear explanation read The Ruby Programming Language by David Flanagan and Yukihiro Matsumoto

like image 165
Endre Simo Avatar answered Nov 03 '22 16:11

Endre Simo


Not very exactly. There is another concept hidden here, called metaclass or eigenclass. The class method is inherited from eigenclass. See Ruby Hacking Guide for more information about it. ( Just search for "class methods" in the page if you don't want to read them all. )

like image 37
halfelf Avatar answered Nov 03 '22 14:11

halfelf