Robert Klemme
4/16/2006 10:11:00 PM
Frank Swarbrick wrote:
> Robert Klemme wrote:
>> Frank Swarbrick wrote:
>>
>>> Given the following, is there anyway for me to call the Mixer method
>>> initMix from within the Mixing method initMix?
>>>
>>> module Mixer
>>> attr :mixup
>>> @@CONST = 123
>>>
>>> def mixedUp
>>> puts "All mixed up!"
>>> end
>>> def initMix
>>> @mixup = "Mix me man!"
>>> end
>>
>>
>> Rather do:
>>
>> def initialize(a*,&b)
>> super
>> @mixup = "Mix me man!"
>> end
>>
>>> end
>>>
>>> class Mixing
>>> include Mixer
>>
>>
>> Remove initialize here as it does nothing:
>>
>>> def initialize
>>> end
>>
>>
>> If it actually does something, do
>>
>> def initialize
>> super( ... arg list of super *class* here ... )
>> # local init
>> end
>>
>>> def show
>>> puts @@CONST
>>> end
>>> # def initMix
>>> # what goes here?
>>> # end
>>> end
>>>
>>> m = Mixing.new
>>> m.show
>>> m.mixedUp
>>> m.initMix
>>> puts m.mixup
>
> That works (except I had to get rid of the parms on initialize), but I'm
> not sure why.
In that case you probably should post the code. The solution I
presented is a solution that will work regardless where you mixin the
module. But you have to invoke super in the derived class with the
super class's (not the mixin's!) signature:
module Mix
def initialize(*a,&b)
super
puts "mix"
end
end
class Base
def initialize(a,b)
puts "base"
end
end
class Derived < Base
include Mix
def initialize(x,y,z)
super(x,z)
puts "derived"
end
end
>And it doesn't really answer the question I'm trying to
> ask. Let's say I add the following to the Mixer module:
> def moreMixedUp
> puts "More Mixed Up"
> end
>
> if I add something similar to the Mixing class:
> def moreMixedUp
> puts "even More Mixed Up"
> end
>
> and then put the following at the end of the program:
> m.moreMixedUp
>
> I will get "even More Mixed Up" as the last output. But I want to be
> able to do something like this in the Mixing class:
> def moreMixedUp
> # call Mixer#moreMixedUp here
> puts "again!"
> end
Just use super.
> I thought perhaps I could put super.moreMixedUp to do it, but that does
You're mixing Java and Ruby. In Ruby it's just "super".
Cheers
robert