Perl有一个名为Carp的模块,它可以用来打印消息(w.o明确地引发异常),它可以打印消息和完整的堆栈跟踪。也就是说。
use Carp qw(cluck) ;
cluck ("foo")会产生:
foo called from file bar, line 2有什么想法吗?如何在Ruby中找到类似的东西?
发布于 2013-11-08 11:54:38
您可以为此使用Kernel#caller_locations (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::Location,所以您也可以从它获得更多信息;查看http://www.ruby-doc.org/core-2.0.0/Thread/Backtrace/Location.html
https://stackoverflow.com/questions/19858374
复制相似问题