Calling a redefined method higher up the ancestor hierarchy

T

Tom Ten Thij

I have a situation where an object has redefined the method method. Is
there a way to call the original method method as provided by the Object
class on it?

Specifically, I would like to make the following spec pass:
http://gist.github.com/312104

Tom.
 
R

Robert Klemme

2010/2/23 Tom Ten Thij said:
I have a situation where an object has redefined the method method. Is
there a way to call the original method method as provided by the Object
class on it?

Specifically, I would like to make the following spec pass:
http://gist.github.com/312104

You can get an unbound method and bind it to the instance:

irb(main):001:0> class TestClass
irb(main):002:1> def foo
irb(main):003:2> :foo
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0> s = TestClass.new
=> #<TestClass:0x1015c230>
irb(main):007:0> desired_method = s.method:)foo)
=> #<Method: TestClass#foo>
irb(main):008:0> class TestClass
irb(main):009:1> def method
irb(main):010:2> raise "I do not want to call this method"
irb(main):011:2> end
irb(main):012:1> end
=> nil
irb(main):013:0>
Object.instance_method:)method).bind(s).call:)foo) == desired_method
=> true

Kind regards

robert
 
T

Tom Ten Thij

Robert said:
You can get an unbound method and bind it to the instance:
Object.instance_method:)method).bind(s).call:)foo) == desired_method

Thanks, that fixed it!

Tom.
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top