views:

62

answers:

1

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

A: 

Check out the book "Metaprogramming Ruby" from the Pragmatic Press. It contains a very detailed explanation of this, in a manner that is very easy to read and understand. http://pragprog.com/titles/ppmetr/metaprogramming-ruby

i can't answer mot of your questions because i don't know python. but the basics of why it's not multiple inheritance is that ruby injects the module into the inheritance chain when the module is included or a class is extended by a module.

class Foo
  include Bar
end

module Bar
end

foo = Foo.new

this produces an inheritance chain where foo is an instance of Foo and Foo inherits from Bar.

it's a bit more tricky than just that, and there are rules for the order in which the inheritance injection occurs. the book i referenced explains it very well.

Derick Bailey
Thanks, hehe, but do you think you could summarize in more detail what the book says? Note: I don't need to know the specifics of how the module is injected, more the theory behind why they chose this approach over the typical multiple inheritance approach and HOW it is different to the normal MI approach. :) Sorry to ask, but I'm not going to buy this book (I already have too many to read hehe).
banister
I've read "Metaprogramming Ruby", and I don't know the answer to banister's question.
Andrew Grimm