Created
January 8, 2013 02:35
-
-
Save adammck/4480587 to your computer and use it in GitHub Desktop.
Track method calls (all of them) for the duration of a block.
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
| def call_spy | |
| calls = [] | |
| depth = 0 | |
| set_trace_func proc { |event, file, line, id, binding, classname| | |
| if id != :set_trace_func | |
| if (event == "c-call" || event == "call") | |
| indent = " " * (depth * 2) | |
| calls << "#{indent}#{classname}##{id}" | |
| depth += 1 | |
| elsif (event == "c-return" || event == "return") | |
| depth -= 1 | |
| end | |
| end | |
| } | |
| yield | |
| calls | |
| ensure | |
| set_trace_func(nil) | |
| 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 "./call_spy" | |
| require "pathname" | |
| calls = call_spy do | |
| Pathname.new(__FILE__).dirname.entries | |
| end | |
| $stderr.puts calls | |
| # Output: | |
| # | |
| # Class#new | |
| # Pathname#initialize | |
| # Kernel#initialize_dup | |
| # String#initialize_copy | |
| # Pathname#dirname | |
| # File#dirname | |
| # Pathname#initialize | |
| # Kernel#initialize_dup | |
| # String#initialize_copy | |
| # Pathname#entries | |
| # Dir#entries | |
| # Dir#open | |
| # Enumerable#to_a | |
| # Dir#each | |
| # Pathname#initialize | |
| # Kernel#initialize_dup | |
| # String#initialize_copy | |
| # Pathname#initialize | |
| # Kernel#initialize_dup | |
| # String#initialize_copy | |
| # Pathname#initialize | |
| # Kernel#initialize_dup | |
| # String#initialize_copy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment