Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Decorate any method in Ruby to get its call stack
def trace_calls_on
scope = {}
trace = TracePoint.new(:call, :line) do |tp|
case tp.event
when :call then puts "#{tp.path}:#{tp.lineno} #{tp.defined_class}::#{tp.method_id} " \
"called from #{scope[:path]}:#{scope[:lineno]} #{scope[:class]}::#{scope[:method_id]}"
when :line then scope = {
event: :line,
lineno: tp.lineno,
path: tp.path,
class: tp.defined_class,
method_id: tp.method_id
}
end
end
trace.enable
yield
trace.disable
end
require 'open-uri'
trace_calls_on do
open('http://google.com', proxy: nil)
end
@toch

This comment has been minimized.

Copy link
Owner Author

commented Sep 8, 2015

I've wrapped the gist into a nice gem with pretty output https://rubygems.org/gems/trace_calls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.