[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

override mixin method

Frank Swarbrick

4/16/2006 5:53:00 AM

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
end

class Mixing
include Mixer
def initialize
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
5 Answers

Robert Klemme

4/16/2006 10:13:00 AM

0

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

HTH

Kind regards

robert

Frank Swarbrick

4/16/2006 9:16:00 PM

0

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. 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

I thought perhaps I could put super.moreMixedUp to do it, but that does
not work. Why does the super call in Mixing#initialize call
Mixer#initialize, but super.moreMixedUp in Mixing#moreMixedUp does not
call Mixer#moreMixedUp ?

Here is the error I am receiving, by the way:
mixins2.rb:32:in `moreMixedUp': undefined method `moreMixedUp' for
nil:NilClass (NoMethodError)
from mixins2.rb:44

Frank

Robert Klemme

4/16/2006 10:11:00 PM

0

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

Frank Swarbrick

4/18/2006 1:42:00 AM

0

Robert Klemme wrote:
> Frank Swarbrick wrote:
>
>> 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".

That works. Thanks! Not sure why I didn't try that...

last_permutation

12/30/2009 1:37:00 PM

0

On Dec 30, 8:22 am, "Stewart" <gorta...@gmail.com> wrote:
> <last_permutat...@yahoo.com> wrote in message
>
> news:083e054f-49b2-441a-9496-88beb4f21b38@r24g2000yqd.googlegroups.com...
>
> > On Dec 28, 9:04 pm, The Endeavor <The_Endea...@Nospam.com> wrote:
> >> Why does Egypt do so little for its brother arabs?
>
> > Payouts from the U.S. to put up with Zionazi Israel might begin to
> > explain it some.
>
> IOW, goy scum can be bought.

ZioNazi calling anyone scum; amusing.

ZioNazis invest a lot of money paying off goyim.
Goyim laugh all the way to the bank. Good long-term
investment?