Created
August 6, 2009 14:36
-
-
Save runpaint/163342 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
class Object | |
def backtrace | |
Backtrace.new | |
end | |
end | |
class Backtrace | |
class Line | |
attr_reader :file, :line, :name | |
def initialize(line) | |
/^(?<file>[^:]+):(?<line>\d+):in `(block.+?)?(?<name>[^ ']+)'/u =~ line | |
@file = file.match(/^\((?<ctx>.+)\)$/) ? $~[:ctx] : File.expand_path(file) | |
@line, @name = line.to_i, name.to_sym | |
end | |
def to_s | |
"#{file}:#{line} in `#{name}'" | |
end | |
alias :inspect :to_s | |
end | |
attr_reader :lines | |
include Enumerable | |
def initialize | |
@lines = caller.map { |line| Line.new(line) } | |
.reject{ |line| line.file == File.expand_path(__FILE__) } | |
end | |
def name(idx=0) | |
lines[idx].name | |
end | |
def file(idx=0) | |
lines[idx].file | |
end | |
def line(idx=0) | |
lines[idx].line | |
end | |
def [](idx) | |
lines[idx] | |
end | |
def each | |
lines.each { |line| yield line } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment