Alex Rivera | Logout

When monkey patching an instance method, can you call the overridden method from the new implementation?

Asked 2010-12-17T11:43:08.223
502

Say I am monkey patching a method in a class, how could I call the overridden method from the overriding method? I.e. Something a bit like super

E.g.

class Foo
  def bar()
    "Hello"
  end
end 

class Foo
  def bar()
    super() + " World"
  end
end

>> Foo.new.bar == "Hello World"
Edit
Report

1 Answer

12

Take a look at aliasing methods, this is kind of renaming the method to a new name.

For more information and a starting point take a look at this replacing methods article (especially the first part). The Ruby API docs, also provides (a less elaborate) example.

answered 2010-12-17T11:52:50.403

Your Answer