Created
November 22, 2011 19:35
-
-
Save Mon-Ouie/1386672 to your computer and use it in GitHub Desktop.
This file contains 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
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