Perl有一个名为Carp的模块,从中可以使用它打印出消息(明显抛出异常),该模块可以打印消息和完整的堆栈跟踪。即
use Carp qw(cluck) ;
cluck ("foo")
将产生:
foo called from file bar, line 2
任何想法如何在Ruby中获得类似的东西?
您可以Kernel#caller_locations
为此使用(http://www.ruby-doc.org/core-2.0.0/Kernel.html#method-i-caller_locations)
def cluck(val)
loc = caller_locations.last
puts "#{val} called from file #{loc.path}, line #{loc.lineno}"
end
cluck 1
cluck "hello"
输出:
1 called from file line_of_caller.rb, line 6
hello called from file line_of_caller.rb, line 7
loc
这是Thread :: Backtrace :: Location的实例,因此您还可以从中获取更多信息;查看http://www.ruby-doc.org/core-2.0.0/Thread/Backtrace/Location.html
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句