Skip to content

Instantly share code, notes, and snippets.

@joefiorini
Created June 12, 2009 01:10
Show Gist options
  • Save joefiorini/128362 to your computer and use it in GitHub Desktop.
Save joefiorini/128362 to your computer and use it in GitHub Desktop.
module TraceCalls
def following(*syms, &block)
syms.each do |sym|
hook_method = "__#{sym}__hooked__"
self.class.send :alias_method, hook_method, sym
#private hook_method
self.class.send :define_method, sym do |*args|
puts "==> Calling #{sym} with #{args.inspect}"
result = send "__#{sym}__hooked__", *args
puts "<== result #{result}"
end
end
end
end
class Example
include TraceCalls
def some_method(arg1, arg2)
arg1 + arg2
end
end
ex = Example.new
ex.following :some_method
ex.some_method(4,5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment