开发者

Can I access the binding at the moment of an exception in Ruby

开发者 https://www.devze.com 2023-04-10 06:14 出处:网络
Say I have: begin 2.times do a = 1 1/0 end rescue puts $! debugger end In this example, I want to get the value of a. If a is initialised in the begin block t开发者_开发知识库hen I can access it w

Say I have:

begin
  2.times do
    a = 1
    1/0
  end

rescue
  puts $!
  debugger
end       

In this example, I want to get the value of a. If a is initialised in the begin block t开发者_开发知识库hen I can access it when I rescue. However, in this example, a is block-local. Is there a way to get the binding at the moment of the exception, when I rescue?


Can't you just put another begin,rescue block inside of the do block?


There seems to be a hack possible to do this. It's not very nice though:

class Foo < Exception
  attr_reader :call_binding

  def initialize
    # Find the calling location
    expected_file, expected_line = caller(1).first.split(':')[0,2]
    expected_line = expected_line.to_i
    return_count = 5  # If we see more than 5 returns, stop tracing

    # Start tracing until we see our caller.
    set_trace_func(proc do |event, file, line, id, binding, kls|
      if file == expected_file && line == expected_line
        # Found it: Save the binding and stop tracing
        @call_binding = binding
        set_trace_func(nil)
      end

      if event == :return
        # Seen too many returns, give up. :-(
        set_trace_func(nil) if (return_count -= 1) <= 0
      end
    end)
  end
end

class Hello
  def a
    x = 10
    y = 20
    raise Foo
  end
end
class World
  def b
    Hello.new.a
  end
end

begin World.new.b
rescue Foo => e
  b = e.call_binding
  puts eval("local_variables.collect {|l| [l, eval(l)]}", b).inspect
end

Source: How can I get source and variable values in ruby tracebacks?

0

精彩评论

暂无评论...
验证码 换一张
取 消