Instantly share code, notes, and snippets.

Embed
What would you like to do?
@call_trace = TracePoint.new(:call) do |tp|
Thread.current['backtraces'] ||= []
Thread.current['backtraces'] << {
binding: tp.binding,
method_id: tp.method_id
}
end
@raise_trace = TracePoint.new(:raise) do |tp|
Thread.current['backtraces'].each do |bt|
yourself = bt[:binding].eval('self')
method = yourself.method(bt[:method_id])
arguments = method.parameters.inject({}) do |args, (type, name)|
args.update(name => (bt[:binding].eval("#{name}") rescue nil))
end
puts "(#{yourself.inspect})##{bt[:method_id]}:"
arguments.each do |name, value|
puts " #{name} = #{value.inspect}"
end
end
end
def foo(x, y)
raise
end
def bar(x, y)
foo(y, x)
end
def baz(x, y, z)
bar(x + z, y)
end
@call_trace.enable
@raise_trace.enable
baz(1, :ahi, 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment