Skip to content

Instantly share code, notes, and snippets.

@vinibaggio
Created March 20, 2011 16:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinibaggio/878426 to your computer and use it in GitHub Desktop.
Save vinibaggio/878426 to your computer and use it in GitHub Desktop.
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