Created
July 16, 2013 16:37
-
-
Save jaz303/6010349 to your computer and use it in GitHub Desktop.
Evaluate code in the context from which exception was thrown
This file contains hidden or 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
# http://stackoverflow.com/questions/106920/how-can-i-get-source-and-variable-values-in-ruby-tracebacks | |
class StandardError | |
alias_method :initialize_old, :initialize | |
attr_reader :binding | |
def initialize(*args) | |
initialize_old *args | |
file, line = caller(1).first.split(':')[0,2] | |
line = line.to_i | |
set_trace_func(proc do |ev,f,l,id,binding,classname| | |
if f == file && l == line | |
@binding = binding | |
set_trace_func nil | |
end | |
end) | |
end | |
end | |
begin | |
def foo | |
bar | |
end | |
def bar | |
a = 10 | |
b = 20 | |
raise RuntimeError, "yolo" | |
end | |
foo | |
rescue => e | |
p e.message | |
p e.backtrace | |
p eval("local_variables", e.binding) | |
p eval("a + b", e.binding) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment