Calling a redefined method higher up the ancestor hierarchy

Discussion in 'Ruby' started by Tom Ten Thij, Feb 23, 2010.

  1. Tom Ten Thij

    Tom Ten Thij Guest

    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.
    --
    Posted via http://www.ruby-forum.com/.
    Tom Ten Thij, Feb 23, 2010
    #1
    1. Advertising

  2. 2010/2/23 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


    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

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Feb 23, 2010
    #2
    1. Advertising

  3. Tom Ten Thij

    Tom Ten Thij Guest

    Robert Klemme wrote:
    > 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.
    --
    Posted via http://www.ruby-forum.com/.
    Tom Ten Thij, Feb 23, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    397
    Joseph Kesselman
    Oct 27, 2006
  2. Replies:
    5
    Views:
    330
  3. Marco
    Replies:
    12
    Views:
    611
  4. MiG
    Replies:
    8
    Views:
    146
    Ryan Leavengood
    Oct 19, 2005
  5. M B HONG 20
    Replies:
    2
    Views:
    108
    Thomas 'PointedEars' Lahn
    Dec 13, 2005
Loading...

Share This Page