LoginSignup
12
11

More than 5 years have passed since last update.

呼び出し元のmethod名を取得

Posted at

※ ruby2.0以上

こんなmethodを定義しておく

test.rb
def current_method
  caller_locations(1).first.label
end
test.rb
def hoge
  "#{current_method}_methodだよ"
end

def fuga
  "#{current_method}_methodだよ"
end
pry
pry(main)> hoge
=> "hoge_methodだよ"
pry(main)> fuga
=> "fuga_methodだよ"

上記のようにcurrent_methodの呼び出し元のmethod名を取得できる

12
11
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
11