Created
March 20, 2011 16:23
-
-
Save vinibaggio/878426 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Module.class_eval do | |
def redefine_instance_method(name, &block) | |
old_instance_method = instance_method(name) | |
define_method(name) do | |
block.call(self, old_instance_method) | |
end | |
end | |
def redefine_class_method(name, &block) | |
old_method = method(name) | |
metaclass = class << self; self; end | |
metaclass.send(:define_method, name) do | |
block.call(old_method) | |
end | |
end | |
end | |
class Bla | |
def self.ola | |
puts "Olá!" | |
end | |
def hello | |
puts "Hello!" | |
end | |
end | |
Bla.redefine_instance_method(:hello) do |instance, old_method| | |
puts "instrumented" | |
old_method.bind(instance).call | |
end | |
Bla.redefine_class_method(:ola) do |old_method| | |
puts "yes it is instrumented" | |
old_method.call | |
end | |
b = Bla.new | |
b.hello | |
Bla.ola |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment