Matz supposedly said "mixins could do almost everything multiple inheritance do, without the associated drawbacks" (Matz words)."
First of all, why is Ruby module inclusion not 'multiple inheritance' ? It seems to me there is very little difference between modules and classes. The fact you cannot instantiate a module is irrelevant when it is used as a superclass.
I also know that successive module inclusion forms a single inheritance chain (not tree) extending upwards from the class. But this, to me, is not sufficient to distinguish it from 'multiple inheritance' as the Python multiple inheritance system also "linearizes" the superclass chain (using the C3 algorithm) it's just that Ruby 'linearizing' process is significantly simpler.
So what exactly distinguishes Ruby module mixins from multiple inheritance in say a language like Python? And why don't the arguments behind the Python adoption of the c3 MRO algorithm apply to Ruby? And if they do apply - why did Ruby decide not to adopt this algorithm ?
thanks
Ruby supports only single class inheritance, it does not support multiple class inheritance but it supports mixins.
In Ruby, modules are somewhat similar to classes: they are things that hold methods, just like classes do. However, modules can not be instantiated. I.e., it is not possible to create objects from a module. And modules, unlike classes, therefore do not have a method new .
Modules eliminate the need of multiple inheritance using mixin in Ruby. A module doesn't have instances because it is not a class. However, a module can be included within a class. When you include a module within a class, the class will have access to the methods of the module.
A Ruby module can contain classes and other modules, which means you can use it as a namespace.
With MI, lots of the issues that arise can be trimmed down to implementation details; you can't just talk about "Multiple inheritance" in general, without talking specifics. So I'll be assuming that you mean "C++ multiple inheritance" when you say "multiple inheritance".
The most common problem with Multiple inheritance is the Diamond Problem. If several superclasses on the same level define the same method, how do you know which method is invoked on the subclass?
With modules, this problem is easily answered - the last included module always "wins". You can't include several modules "simultaneously", as you can do with classes in C++. So this problem never arises.
The fact you cannot instantiate a module is irrelevant when it is used as a superclass
I respectfully disagree.
First, modules are never "used as superclasses" in ruby; only superclasses are.
Second, with multiple inheritance, knowing exactly in what order the constructors (& destructors!) are called is not a trivial matter. The fact that ruby modules don't allow instantiation suppresses that problem altogether.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With