alias_method and initialize

L

Leon Bogaert

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!
 
D

Day

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
 
J

Jari Williamsson

Leon said:
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...

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

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



Best regards,

Jari Williamsson
 
S

Siep Korteling

Leon said:
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"
 
P

Phlip

Leon said:
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...

When a class or module evaluates, like Extras, all its top-level expressions run
"while" the interpreter is ... interpreting between its 'module' and 'end'
statements. This interpretation creates the resulting Extras object, which can
then be used as a module.

At the time alias_method called, Extras was not yet connected to Test.

I can't suggest a work-around until I know what problem you are trying to solve!
But if Extra simply declared an initialize method, alone, then at 'include' time
it would blot out the one in Test...
 
D

Day

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
 
M

Matt Mower

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?

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 (http://pastie.caboo.se/150269):

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top