我希望能夠從當前調用的方法中識別出方法的名稱。我曾嘗試以下:self.name返回錯誤? Ruby1.9
#!/usr/bin/env ruby
class SomeClass
def initialize
puts self.name
end
end
p = SomeClass.new
但收到以下錯誤:
./test.rb:4:in `initialize': undefined method `name' <SomeClass:0x007fe4d107ba30 (NoMethodError)
from ./test.rb:7:in `new'
from ./test.rb:7:in `<main>'
我怎麼叫我從調用方法?
就像頭一起,有類#名稱的方法,這是你想要的無關。 – 2012-03-18 22:32:48
[在Ruby中獲取當前正在執行的方法的名稱]的可能重複(http://stackoverflow.com/questions/199527/get-the-name-of-the-currently-executing-method-in-ruby) – 2014-09-11 08:02:04