3 回答

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超4個(gè)贊
在Ruby 2.0.0中,您可以使用:
caller_locations(1,1)[0].label
它比Ruby 1.8+解決方案快得多:
caller[0][/`([^']*)'/, 1]
backports當(dāng)我有時(shí)間(或請(qǐng)求請(qǐng)求?。r(shí)將包含在其中。

TA貢獻(xiàn)2037條經(jīng)驗(yàn) 獲得超6個(gè)贊
相反,您可以將其編寫為庫函數(shù),并在需要時(shí)進(jìn)行調(diào)用。代碼如下:
module CallChain
def self.caller_method(depth=1)
parse_caller(caller(depth+1).first).last
end
private
# Copied from ActionMailer
def self.parse_caller(at)
if /^(.+?):(\d+)(?::in `(.*)')?/ =~ at
file = Regexp.last_match[1]
line = Regexp.last_match[2].to_i
method = Regexp.last_match[3]
[file, line, method]
end
end
end
要觸發(fā)上述模塊方法,您需要這樣調(diào)用: caller = CallChain.caller_method
- 3 回答
- 0 關(guān)注
- 689 瀏覽
添加回答
舉報(bào)