开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜