Created
October 15, 2012 13:34
-
-
Save bittersweet/3892492 to your computer and use it in GitHub Desktop.
Wrapping an instance method
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 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