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

ruby monkey patch class method
ruby monkey patch private method
rails override gem method
ruby prepend
monkey-patch vs override
ruby extend class from gem
ruby prepend monkey patch
ruby prepend class method

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"

The Art of Rails, When monkey patching an instance method, can you call the overridden method from the new implementation? (3 answers). Closed 6 months  Now every time when you call the method defined inside the module, it calls your own method. You changed the behavior of the method defined in the module. Conclusion. This is all about monkey patching in Python- a technique to change the behavior of class or module.

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.

Monkey patch ruby instance method, using the original definition , Calls on mock objects are specified with the method() function: def The implementation with PyMock largely mirrors the pMock implementation, Monkeypatching eliminates the necessity of introducing a second variable to hold the mock. Instead, the mocked method is attached directly to an instance using the override()  Monkey patching can only be done in dynamic languages, of which python is a good example. Changing a method at runtime instead of updating the object definition is one example;similarly, adding attributes (whether methods or variables) at runtime is considered monkey patching.

The class that will make override must to be reloaded after class that contains the original method, so require it in the file that will make overrride.

Foundations of Agile Python Development, If you don't know #super it is a function of inheritance and composition that allows an overwriting method to call “up” the inheritance chain to the Class or Module it is It is often used when monkey patching an existing method to tailor its you to alter existing code functionality without writing any new code  As of PHP 5.6, there's still no support for monkey patching; however PHP 5.3 introduced anonymous functions. This answer is not exactly what you're looking for and could probably be improved upon, but the general idea is to use arrays, anonymous functions, and references to create a self-contained, self-referential array (an "object", if you will):

Prepend and Super for elegant Monkey Patching, You don't have to call Time.now.advance(days: -1) , you can write 1.day.ago ! If two libraries monkey-patch the same method, you won't be able to tell. And they'll actually use those convenient new methods you wrote, instead of those of overrides (instead of additions) and especially overrides of class methods. The self is a reference to the current instance the method is bound to. It is not used when you just call something like print which access any properties or methods attached to that instance. But for a different case please have a look at the following example:

3 ways to monkey-patch without making a mess, Monkey patching is useful when you find yourself wishing an existing to the class definition itself, it's as easy as calling the method on any string. class String def titleize # your implementation here end end. If When it's open, any definitions you make inside will be available to all instances of the class. print(instA.testfunc) #<bound method B.testfuncPatch of <__main__.B object at 0x1056ab6d8>> so the method basically is treated as a staticmethod meaning you would have to call it with the instance as the first argument: instA.testfunc(instA,"keep away!

Make Monkey Patching in Ruby Less Risky with Refinements, Within this section I outline how to monkey patch while providing tactics on Fortunately Ruby allows you to query the methods of an object and the instance methods of a Checking that a class or module exists before you implement ensures clarity Within our new #capitalize method we can call the code of the original  Groovy Monkey Patching This Context. Hey Groovers! I have a delicat question. It is about the 'use-magic'. It's often called monkey patching in general terms, or as groovy it calls 'pimp my

Comments
  • Shouldn't the first Foo class be some other and the second Foo inherit from it?
  • nope, i'm monkey patching. I was hoping there would be something like super() I could use to call the original method
  • This is needed when you don't control the creation of Foo and the usage of Foo::bar. So you have to monkey patch the method.
  • @Jörg W Mittag, is the method wrapping approach thread safe? What happens when two concurrent threads call bind on the same old_method variable?
  • @KandadaBoggu: I'm trying to figure out what exactly you mean by that :-) However, I'm pretty sure it is no less thread-safe than any other kind of metaprogramming in Ruby. In particular, every call to UnboundMethod#bind will return a new, different Method, so, I don't see any conflict arising, regardless of whether you call it twice in a row or twice at the same time from different threads.
  • Was looking for an explanation on patching like this ever since I started on ruby and rails. Great answer! The only thing missing for me was a note on class_eval vs. reopening a class. Here it is: stackoverflow.com/a/10304721/188462
  • Ruby 2.0 has refinements blog.wyeworks.com/2012/8/3/ruby-refinements-landed-in-trunk
  • Where do you find old and redef? My 2.0.0 doesn't have them. Ah, it's hard not to miss the Other competing ideas that did not make it into Ruby were: