Skip to content

Instantly share code, notes, and snippets.

@Mon-Ouie
Created November 22, 2011 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mon-Ouie/1386672 to your computer and use it in GitHub Desktop.
Save Mon-Ouie/1386672 to your computer and use it in GitHub Desktop.
require 'binding_of_caller'
BacktraceEntry = Struct.new(:file, :line, :method_name) do
def to_s
"#{file}:#{line}: " + (method_name ? "in #{method_name}" : "at toplevel")
end
end
def bindings_before(b)
ary = []
(0..Float::INFINITY).each do |n|
ary << b.of_caller(n)
end
rescue # raises a plain RuntimeError when it fails
ary
end
def caller
bindings_before(binding.of_caller(0)).map do |b|
BacktraceEntry.new(b.eval("__FILE__"),
b.eval("__LINE__"),
b.eval("__method__")) # hope the method hasn't changed
end
end
def foo
loop do
puts caller
break
end
end
foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment