Last active
September 8, 2015 14:50
-
-
Save toch/82fd93ca449500d21f00 to your computer and use it in GitHub Desktop.
Decorate any method in Ruby to get its call stack
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 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've wrapped the gist into a nice gem with pretty output https://rubygems.org/gems/trace_calls