开发者

Can I make ruby display method parameter values in a backtrace?

开发者 https://www.devze.com 2023-01-13 11:48 出处:网络
Using MRI 1.开发者_Go百科9 When an exception is raised that causes a backtrace to be printed, it would often be immensely easier to debug if the backtrace showed the receiver and values of method par

Using MRI 1.开发者_Go百科9

When an exception is raised that causes a backtrace to be printed, it would often be immensely easier to debug if the backtrace showed the receiver and values of method parameters as well as the method name. Is there any way of doing this?

Think about situations such as passing a nil deep into library code that wasn't expecting it, or where two strings have incompatible encodings and some routine is trying to concatenate them


you can with 1.8.6 by using the backtracer gem.

1.9 has slightly broken callbacks so isn't compatible yet, though. I might be able to get it to work, if desired.

You could use something like a delegate and see the parameters to a single object:

class A
  def go a, b
  end
end

class A2

  def initialize *args
    @delegate = A.new *args
  end

  def method_missing meth, *args
    p "got call to #{meth}", args.join(', ')
    @delegate.send(meth,*args)
  end

end

which outputs

"in go2"
"got call to go"
"3, 4"
0

精彩评论

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