From: Paul Brannan Date: 2001-10-06T06:31:32+09:00 Subject: [ruby-talk:22143] Re: Ruby and multiple inheritance On Sat, 6 Oct 2001, Alan Stern wrote: > Here's how to do multiple inheritance in the Ruby framework. Instead > of defining a bunch of classes, making up some inheritance tree, > define your classes as modules. So instead of having a class C, you > will have a module MC. By including modules in other modules, you can > effectively set up a multiple-inheritance network. That is, if you > wanted to have class C derived from parents B1 and B2, you would > simply have module MC include both modules MB1 and MB2. Finally, in > order to make instances of MC, just define a dummy class MC_ that > includes MC but has no other structure, and instantiate that. This isn't quite the same as multiple inheritance. For starters, every time you include a new module, you are creating an anonymous proxy class that the current class inherits from. This is different from MI, because while MI can create "the diamond pattern" and tree shapes and other crazy things, using a mixin is very linear. As you stated this becomes apparent (and particularly annoying) when you have two mixins, each with a method of the same name. With real MI, you would have two separate methods in two separate classes, each callable from the derived class (though you may have to clarify which one in order for you compiler/iterpreter to know). Not being able to specify which method to call seems limiting. Additionally, mixins generally do not carry their own instance data, and there's no well-defined way to initialize instance data in a mixin (with inerhitance in Ruby, you initialize the base class with super()). And what happens when two mixins use an instance variable with the same name? I can tell you the results are not good. I've found that not thinking of mixins as a replacement for MI has been beneficial to my health. Every mixin I write is named with an adjective, has a very clear, well-defined private and public interface, and I am very careful when I use instance variables in a mixin. This, I think, has greatly reduced my intake of ibuprofen. Paul