Rubyコードフラグメント:現在のクラス名を取得

1204 ワード

bigboldで見た
Get the currently running method name in Rubyコードフラグメント:
module Kernel private    
def
 this_method_name      caller[0] 
=~
 
/
`([
^
'
]*)
'
/
 
and
 $
1
    endend
class
 Foo 
def
 test_method   this_method_name endendputs Foo.new.test_method    
#
 => test_method
これに対して
caller[0] 
=~
 
/
`([
^
'
]*)
'
/
 
and
 $
1このような文法の構造はとても理解できないで、後でまたこのように書いてもいいcaller[0][/`([^']*)'/,1]を発見して、そこで参考のハンド・バックの中で調べてみます
callerの使い方は、まあまあ分かります。
caller
[ファイル名、行番号、メソッド名]を返します。