我需要在Ruby中获取一个堆栈跟踪对象,而不是打印它,只是为了让它做一些记录和转储以便以后分析。这有可能吗?多么?
发布于 2010-09-30 17:16:03
您可以使用Kernel.caller来实现这一点。在为异常生成堆栈跟踪时也使用相同的方法。
从文档中:
def a(skip)
caller(skip)
end
def b(skip)
a(skip)
end
def c(skip)
b(skip)
end
c(0) #=> ["prog:2:in `a'", "prog:5:in `b'", "prog:8:in `c'", "prog:10"]
c(1) #=> ["prog:5:in `b'", "prog:8:in `c'", "prog:11"]
c(2) #=> ["prog:8:in `c'", "prog:12"]
c(3) #=> ["prog:13"]
发布于 2013-07-31 18:48:24
试一试
Thread.current.backtrace.join("\n")
发布于 2010-09-30 17:01:36
# Returns any backtrace associated with the exception.
# The backtrace is an array of strings, each containing either ``filename:lineNo: in `method’’’ or ``filename:lineNo.’‘
def a
raise "boom"
end
def b
a()
end
begin
b()
rescue => detail
print detail.backtrace.join("\n")
end
产生:
prog.rb:2:in `a'
prog.rb:6:in `b'
prog.rb:10
https://stackoverflow.com/questions/3829157
复制相似问题