From: Justin Collins Date: 2008-04-30T04:03:15+09:00 Subject: Re: Mixins Marcin Tyman wrote: > Hi guys!!! > My question is: > > Is any way to define variables in module which can be further used in > mixin? The problem is that module I wanted to include into class has > constant variables defined which are used within function it implements. > I'm looking for simplest way to overwrite the variables from the class > (may change the variables to non constant ones) the same meaning not to > change implementation of functions from the module. See: > > module Mod > AnyVar = 10 > > def func > #the method use AnyVar > end > end > > class MyClass > include Mod > > def initialize > #read configuration > > #how to make the function use new AnyVar value without changing its > implementation and parameters list > func() > end > end > > > Thanks a lot for any ideas. > I am not sure I quite understand what you are trying to do, but you can do something like this: module A def initialize super @var = 1 end end class B include A def initialize super end def check_var puts @var end end B.new.check_var #=> 1 The trouble is you need to make sure you call 'super' religiously if you are including a lot of modules. -Justin