Created
March 5, 2011 10:46
-
-
Save bakineggs/856288 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| class Module | |
| def trace_callers method | |
| alias_method :"#{method}_without_trace", method | |
| define_method method do |*args, &blk| | |
| puts "Invocation of #{method}:" | |
| puts caller.map {|line| " #{line}"} | |
| send :"#{method}_without_trace", *args, &blk | |
| end | |
| end | |
| end |
This file contains hidden or 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
| require 'trace_callers' | |
| class Foo | |
| trace_callers :initialize | |
| def bar | |
| yield | |
| end | |
| trace_callers :bar | |
| end | |
| def outer | |
| inner Foo.new | |
| end | |
| def inner foo | |
| foo.bar { puts "Cool!" } | |
| end | |
| outer | |
| # Ouputs the following: | |
| # | |
| # Invocation of initialize: | |
| # trace_callers_example.rb:13:in `new' | |
| # trace_callers_example.rb:13:in `outer' | |
| # trace_callers_example.rb:20 | |
| # Invocation of bar: | |
| # trace_callers_example.rb:17:in `inner' | |
| # trace_callers_example.rb:13:in `outer' | |
| # trace_callers_example.rb:20 | |
| # Cool! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment