Created
January 18, 2009 16:47
-
-
Save voldern/48697 to your computer and use it in GitHub Desktop.
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 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) |
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
: 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