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?
-
There are the hooks
method_addedandmethod_undefined. Garry Dolley has written an Immutable module that prevents monkey patching. -
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
0 comments:
Post a Comment