Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does ruby support multiple inheritance? [closed]

Tags:

ruby

How does ruby support multiple inheritance so that I can inherit from multiple classes?

like image 940
Joe Avatar asked Dec 15 '12 02:12

Joe


People also ask

How does Ruby achieve multiple inheritance?

In order to achieve multiple inheritance, Ruby provides something called mixins that one can make use of. Inheritance helps in improving the code reusability, as the developer won't have to create the same method again that has already been defined for the parent class.

Which inheritance is not supported in Ruby?

Multiple inheritance - This is absolutely not possible in ruby not even with modules.

How does inheritance work in Ruby?

Inheritance is when a class inherits behavior from another class. The class that is inheriting behavior is called the subclass and the class it inherits from is called the superclass. We use inheritance as a way to extract common behaviors from classes that share that behavior, and move it to a superclass.

How is multiple inheritance implemented in Rails?

Multiple inheritance is a feature that allows one class to inherit from multiple classes(i.e., more than one parent). Ruby does not support multiple inheritance. It only supports single-inheritance (i.e. class can have only one parent), but you can use composition to build more complex classes using Modules.


1 Answers

Ruby does not directly have multiple inheritance. Ruby has something similar though: mixins. For example:

module M; end
module N; end

class C
  include M
  include N
end

C.ancestors  #=>  [C, N, M, Object, Kernel, BasicObject]

Note that mixins are not multiple inheritance, but instead mostly eliminate the need for it.

like image 128
Andrew Marshall Avatar answered Sep 28 '22 18:09

Andrew Marshall