|
|
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!
--
Posted via http://www.ruby-forum.com/.
|
|
0
|
|
|
|
Reply
|
leon160 (34)
|
2/8/2008 10:01:29 PM |
|
On Feb 8, 2008 4:01 PM, Leon Bogaert <leon@tim-online.nl> 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
|
|
0
|
|
|
|
Reply
|
iamday (28)
|
2/8/2008 10:20:45 PM
|
|
Leon Bogaert 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:
> 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
|
|
0
|
|
|
|
Reply
|
jari.williamsson (132)
|
2/8/2008 10:31:35 PM
|
|
Leon Bogaert 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"
--
Posted via http://www.ruby-forum.com/.
|
|
0
|
|
|
|
Reply
|
s.korteling (175)
|
2/8/2008 10:46:34 PM
|
|
Leon Bogaert wrote:
> 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...
--
Phlip
|
|
0
|
|
|
|
Reply
|
phlip2005 (2147)
|
2/8/2008 10:53:44 PM
|
|
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.
--
Posted via http://www.ruby-forum.com/.
|
|
0
|
|
|
|
Reply
|
leon160 (34)
|
2/9/2008 8:36:26 PM
|
|
On Feb 9, 2008 2:36 PM, Leon Bogaert <leon@tim-online.nl> 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
|
|
0
|
|
|
|
Reply
|
iamday (28)
|
2/9/2008 8:48:59 PM
|
|
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?
--
Posted via http://www.ruby-forum.com/.
|
|
0
|
|
|
|
Reply
|
leon160 (34)
|
2/9/2008 10:35:43 PM
|
|
Anyone?
--
Posted via http://www.ruby-forum.com/.
|
|
0
|
|
|
|
Reply
|
leon160 (34)
|
2/10/2008 9:56:51 PM
|
|
On 2/9/08, Leon Bogaert <leon@tim-online.nl> wrote:
> 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.
--
Matt Mower :: http://matt.blogs.it/
|
|
0
|
|
|
|
Reply
|
matt.mower (103)
|
2/11/2008 9:36:38 AM
|
|
Works perfectly! I thought I tried that already... guess not.
Thanks Matt!
--
Posted via http://www.ruby-forum.com/.
|
|
0
|
|
|
|
Reply
|
leon160 (34)
|
2/11/2008 10:00:47 AM
|
|
|
10 Replies
40 Views
(page loaded in 0.181 seconds)
|
|
|
|
|
|
|
|
|