Skip to content

Instantly share code, notes, and snippets.

@voldern
Created January 18, 2009 16:47
Show Gist options
  • Save voldern/48697 to your computer and use it in GitHub Desktop.
Save voldern/48697 to your computer and use it in GitHub Desktop.
module TraceCalls
def self.included(klass)
def klass.method_added(method)
return if @_meta_yo
@_meta_yo = true
alias_method :"#{method} old", method
define_method(method) do |*args|
puts "--> Calling #{method} with #{args.inspect}"
out = send(:"#{method} old", *args)
puts "<-- Returned #{out.inspect}"
out
end
@_meta_yo = false
end
end
end
class Test
include TraceCalls
def test(a)
a
end
def calc(a, b)
a + b
end
end
test = Test.new
puts test.test('asd')
puts test.calc(5, 6)
: voldern@harry ~/Kode/ruby/lek > ruby trace.rb
--> Calling test with ["asd"]
<-- Returned "asd"
asd
--> Calling calc with [5, 6]
<-- Returned 11
11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment