Sander Land
9/12/2006 7:31:00 PM
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
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 A
I am A
I am B
I am A