Skip to content

Instantly share code, notes, and snippets.

@runpaint
Created August 6, 2009 14:36
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 runpaint/163342 to your computer and use it in GitHub Desktop.
Save runpaint/163342 to your computer and use it in GitHub Desktop.
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