本文簡要介紹ruby語言中 Kernel.caller
的用法。
用法
caller(start=1, length=nil) → array or nil
caller(range) → array or nil
返回當前執行堆棧——一個包含 file:line
或 file:line: in `method'
形式的字符串的數組。
可選的start
參數確定要從堆棧頂部省略的初始堆棧條目數。
第二個可選的length
參數可用於限製從堆棧返回的條目數。
如果 start
大於當前執行堆棧的大小,則返回 nil
。
您可以選擇傳遞一個範圍,該範圍將返回一個包含指定範圍內條目的數組。
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:in `<main>'"]
c(1) #=> ["prog:5:in `b'", "prog:8:in `c'", "prog:11:in `<main>'"]
c(2) #=> ["prog:8:in `c'", "prog:12:in `<main>'"]
c(3) #=> ["prog:13:in `<main>'"]
c(4) #=> []
c(5) #=> nil
相關用法
- Ruby Kernel.catch用法及代碼示例
- Ruby Kernel.class用法及代碼示例
- Ruby Kernel.clone用法及代碼示例
- Ruby Kernel.local_variables用法及代碼示例
- Ruby Kernel.Integer用法及代碼示例
- Ruby Kernel.binding用法及代碼示例
- Ruby Kernel.frozen?用法及代碼示例
- Ruby Kernel.`cmd`用法及代碼示例
- Ruby Kernel.autoload用法及代碼示例
- Ruby Kernel.loop用法及代碼示例
- Ruby Kernel.Hash用法及代碼示例
- Ruby Kernel.set_trace_func用法及代碼示例
- Ruby Kernel.exit!用法及代碼示例
- Ruby Kernel.trap用法及代碼示例
- Ruby Kernel.String用法及代碼示例
- Ruby Kernel.select用法及代碼示例
- Ruby Kernel.syscall用法及代碼示例
- Ruby Kernel.then用法及代碼示例
- Ruby Kernel.sprintf用法及代碼示例
- Ruby Kernel.Pathname用法及代碼示例
- Ruby Kernel.srand用法及代碼示例
- Ruby Kernel.yield_self用法及代碼示例
- Ruby Kernel.BigDecimal用法及代碼示例
- Ruby Kernel.raise用法及代碼示例
- Ruby Kernel.test用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Kernel.caller。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。