From: Adam Prescott Date: 2011-01-05T21:26:13+09:00 Subject: Re: mixin technique overrides variables --90e6ba615092ecb3290499187c92 Content-Type: text/plain; charset=UTF-8 It works this way because when you include Parent1, you give its initialize method to the Child class, so when you create a new instance of Child, it calls Child#initialize, which exists after the include and therefore Parent2#initialize in the inheritance chain is never needed. Without the include, Child#initialize ends up at Parent2#initialize via inheritance. On Wed, Jan 5, 2011 at 12:10 PM, Konrad K. wrote: > Take a look at code bellow: > > module Parent1 > def initialize > @loc1 = 1 > @loc2 = 2 > end > end > > class Parent2 > def initialize > @loc3 = 3 > @loc4 = 4 > end > > def displayp2 > puts @loc3 > puts @loc4 > end > end > > class Child < Parent2 > include Parent1 > def displayc > puts @loc1 > puts @loc2 > puts @loc3 > puts @loc4 > end > end > > > c = Child.new > c.displayc > > > I was thinking of something similar to multiple inheritance and my > expectaions was that all of local variables will be available in Child > class. > What I discovered that when you add > include Parent1 > line to Child class @loc3 and @loc4 disappears. > When you comment this line @loc3 and @loc4 are available in Child class. > > My question is why this work that way and how to get all of @loc > available in Child class? > > -- > Posted via http://www.ruby-forum.com/. > > --90e6ba615092ecb3290499187c92--