Skip to content

Instantly share code, notes, and snippets.

@adammck
Created January 8, 2013 02:35
Show Gist options
  • Save adammck/4480587 to your computer and use it in GitHub Desktop.
Save adammck/4480587 to your computer and use it in GitHub Desktop.
Track method calls (all of them) for the duration of a block.
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
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