hello,
Say I have the following Ruby code.
Class A
def test
puts "I am A"
end
end
Class B < A
def test
puts "I am B"
end
end
Class C < B
def test
puts "I am C"
end
end
Now, in the function test in Class C, I want to call the test method defined
in class A, by passing B's test method.
How can I do this, just using plain 'super' doesn't work? Is it possible to do
this?
Either way, please help
Thanks in advance,
···
--
Surendra Singhi
http://ssinghi.kreeti.com, http://www.kreeti.com
Read my blog at: http://cuttingtheredtape.blogspot.com/
,----
Great wits are sure to madness near allied,
And thin partitions do their bounds divide.
(John Dryden, Absalom and Achitophel, 1681)
`----
Use instance_method and bind.
module Kernel
def super_n(n=1)
self.class.ancestors[n].instance_method(caller.first[/`(.*)'/][1..-2]).bind(self).call
end
end
class C < B
def test
puts "I am C"
A.instance_method(:test).bind(self).call
self.class.superclass.superclass.instance_method(:test).bind(self).call
self.class.ancestors[2].instance_method(:test).bind(self).call
super_n(1)
super_n(2)
end
end
C.test.new
=>
I am C
I am A
I am B
I am A
···
On 9/12/06, Surendra Singhi <efuzzyone@netscape.net> wrote:
Now, in the function test in Class C, I want to call the test method defined
in class A, by passing B's test method.
How can I do this, just using plain 'super' doesn't work? Is it possible to do
this?
"Sander Land" <sander.land@gmail.com> writes:
···
On 9/12/06, Surendra Singhi <efuzzyone@netscape.net> wrote:
Now, in the function test in Class C, I want to call the test method defined
in class A, by passing B's test method.
How can I do this, just using plain 'super' doesn't work? Is it possible to do
this?
Use instance_method and bind.
module Kernel
def super_n(n=1)
self.class.ancestors[n].instance_method(caller.first[/`(.*)'/][1..-2]).bind(self).call
end
end
Super cool, thanks a lot.
--
Surendra Singhi
http://ssinghi.kreeti.com, http://www.kreeti.com
Read my blog at: http://cuttingtheredtape.blogspot.com/
,----
"War is Peace! Freedom is Slavery! Ignorance is Strength!"
-- Orwell, 1984, 1948
`----