How do you check that monkey patching has been done to a specific class in Ruby? If that is possible, is it also possible to get the previous implementation(s) of the attribute that's been patched?
Asked
Active
Viewed 876 times
8
-
Might be related to my ?: http://stackoverflow.com/questions/175655/how-to-find-where-a-ruby-method-is-defined-at-runtime - if someone figures this out, pls let me know too! – Matt Rogish Dec 02 '08 at 21:39
2 Answers
8
There are the hooks method_added
and method_undefined
. Garry Dolley has written an Immutable module that prevents monkey patching.

user37011
- 797
- 4
- 7
4
I found this blog posting that touches on how to use method_added to track monkey patching. It's not too hard to extend it to track the methods that were patched.
http://hedonismbot.wordpress.com/2008/11/27/monkey-business-2/:
By using open classes, we can re-define method_added for instances of Class and do some custom stuff every time a method is defined for any class. In this example, we’re re-defining method_added so that it tracks where the method was last defined.
#!/usr/bin/env ruby class Class @@method_history = {} def self.method_history return @@method_history end def method_added(method_name) puts "#{method_name} added to #{self}" @@method_history[self] ||= {} @@method_history[self][method_name] = caller end def method_defined_in(method_name) return @@method_history[self][method_name] end end