Hi,
I’m implementing an application which tracks all methods which are added
to a class. So I need to be notified everytime a new method gets added
to a class. I do it this way:
class Class
def singleton_method_added(method_name)
puts "new singleton method added
do_something_with_this_info(self, method_name)
end
def method_added(method_name)
puts "new method added"
do_something_with_this_info(self, method_name)
end
end
Very simple, but very effective yet.
However, I have one concern doing it this way: When another Ruby
library/application also implements method_added/singleton_method_added
in the class Class, then my implementation would be overridden. This is
very bad since my whole application relies on the code above. I know
that I can’t prevent other applications from overriding and thats ok
since I don’t want to break the implementation of those applicatins.
However, at least I need to be notified when
method_added/singleton_method_added methods get overridden so that I can
react properly.
So my question: Does some kind of hook/callback exist in Ruby which gets
called each time a method has been redefined?