From: Robert Klemme Date: 2003-12-04T05:22:08+09:00 Subject: Re: mixin vs. multiple inheritence "Paul Brannan" schrieb im Newsbeitrag news:20031203174412.GW11423@atdesk.com... > On Thu, Dec 04, 2003 at 02:37:06AM +0900, Robert Klemme wrote: > > > You can build as complicated a mixin graph as you can a MI graph. The > > > difference is that because mixins are modules, they don't have their > > > own state, but instead parasitically use the state of the class into > > > which they are mixed. > > > > U sure about that? Try this: > > > > > p X.new.instance_variables > > I think you misunderstood what Dave was saying. I don't think so; maybe I wasn't clear enough. The error is in the sentence "The difference is that because mixins are modules, they don't have their own state, but instead parasitically use the state of the class into which they are mixed." First, when talking about "their own state" this refers strictly speaking to the class and module instance themself. But I think, this is not what Dave wanted to say. Dave wanted to talk about the state of *instances* that are not classes or modules. Now the scene becomes clearer: modules don't use the state of the class, they even don't use the state that a class *defined* ("defined" as in C++ and Java, where the class defines instance variables). That's because neither classes nor modules define the state of an instance. Instance variables come into existence by executing code (i.e. methods), and they are bound to the instance to which the method belongs. It doesn't matter which class or module defined the method. Important is the runtime instance at hand. So Ruby avoids the typical MI problems simply by not letting classes define instance variables. There's no space allocation for instance variables because of a class definition. There's just a bunch o methods that are executed sooner or later. "initialize" is not special with respect to instance variables: it's just automatically executed on instance creation. Neither classes nor modules define their own scope at the moment (that would only change with @_var). Classes and modules in Ruby have very little differences. The main difference beeing that there is only single inheritance between classes while there is multiple inheritance with modules. Additionally the syntax to define these inheritance relationships is different. > This prints [ "@foo", "@bar" ]. Right. > That's because the module and the class in which it is > included share the same state, exactly as Dave pointed out. Hopefully I have made clearer what I was up to... Kind regards robert