Skip to content

Instantly share code, notes, and snippets.

@bittersweet
Created October 15, 2012 13:34
Show Gist options
  • Save bittersweet/3892492 to your computer and use it in GitHub Desktop.
Save bittersweet/3892492 to your computer and use it in GitHub Desktop.
Wrapping an instance method
module Instrument
def instrument(method)
alias_method "#{method}_old", "#{method}"
define_method method do |*args|
t = Time.now.to_i
send("#{method}_old", *args)
result = Time.now.to_f - t
p "#{method}: #{result} seconds"
end
end
end
class Calculator
extend Instrument
def five_times(n)
p 5 * n
end
instrument :five_times
end
Calculator.new.five_times(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment