Alias_method and initialize

Hi all!

I’ve got a question about using alias_method with a class and a module.
The module gets included by the class. But I can’t figure out how to
deal with the initialize method.

This is my script with output:
http://pastie.caboo.se/149491

Why isn’t tests[1] filled? I tried instance_eval and stuff but I don’t
exactly understand what I’m supposed to do…

Thanks in advance!

On Feb 8, 2008 4:01 PM, Leon B. [email protected] wrote:

Why isn’t tests[1] filled? I tried instance_eval and stuff but I don’t
exactly understand what I’m supposed to do…

I could be wrong (of course!), but it looks to me like the aliasing
isn’t working the way you think it should. Rather than
initialize_with_extras being triggered and then triggering Test’s
initialize, only Test’s initialize is being hit. Hrm. What happens if
you call initialize_with_extras just initialize and create a conflict?
Or maybe something like this?

module Extras
alias_method :initialize, :initialize_without_extras

def initialize
initialize_without_extras #Do it!
@tests[2] = ‘ok too!’
end
end

I’m sort of just guessing.

Ben

Leon B. wrote:

Why isn’t tests[1] filled? I tried instance_eval and stuff but I don’t
exactly understand what I’m supposed to do…
I do not understand what you are trying to achieve. Anyway, tests[1] is
“ok”. Arrays are zero based, the counting starts at zero.
ar=[“a”,“b”]
p ar[0]

returns “a”

Leon B. wrote:

I’ve got a question about using alias_method with a class and a module.
The module gets included by the class. But I can’t figure out how to
deal with the initialize method.

This is my script with output:
Parked at Loopia

Why isn’t tests[1] filled? I tried instance_eval and stuff but I don’t
exactly understand what I’m supposed to do…

The code in the module will not be called at all.

alias_method() does not rename methods, it creates aliases.

Best regards,

Jari W.

On Feb 9, 2008 2:36 PM, Leon B. [email protected] wrote:

the initialize method isn’t overwritten.

I think this is because the class has “overwritten” the initialize
method from the module (sort of how a child class will do regarding
its parent). Hrm hrm hrm. basically, when you call test.new it goes to
the Test class and says, “Okay. Do i have an initialize method here?
Ah! Yes. Here it is.” And runs it. If it didn’t find one, it would
kick up to the module’s initialize method, or if the initialize method
for the class called on the module’s… Remember that the chain
looking for methods trickles UP the inheritence chain and that
including modules is a similar chain.

Sorry I don’t have another suggestion, but maybe that’ll point you in
the right direction?

Ben

I tried something new:

http://pastie.caboo.se/149810

But I get an error. Does anyone know why initialize_with_extras isn’t
defined at that point?

I updated the code (made it simpler) but it still doesn’t do what I
expect it to do:
http://pastie.caboo.se/149769

the initialize method isn’t overwritten.

Anyone?

On 2/9/08, Leon B. [email protected] wrote:

I tried something new:

Parked at Loopia

But I get an error. Does anyone know why initialize_with_extras isn’t
defined at that point?

Your problem, as I understand it, is that included is called in the
context of the class Test and not any of it’s instances.

Try this (Parked at Loopia):

module Extras

def initialize_with_extras
initialize_without_extras #Do it!
@tests[2] = ‘ok too!’
end

def self.included( base )
base.send( :alias_method, :initialize_without_extras, :initialize
)
base.send( :alias_method, :initialize, :initialize_with_extras )
end

end

Output:

[“ok!”, “ok too!”]

It uses the “included” method in the module to amend the class and
setup the initialize alias chain that you need.

Hope this helps.

Regards,

Matt.

Works perfectly! I thought I tried that already… guess not.

Thanks Matt!