Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby | Kernel | __callee__(__method__)

概要

Kernel#__callee__(__method__)

詳細

現在のメソッド名を返します。 メソッドの外で呼ばれると nil を返します。

現在のメソッドエイリアスの場合
__callee__はエイリアス
__method__は元のメソッド
それぞれ返却します

サンプルコード
# encoding: utf-8
require 'tbpgr_utils'

class Hoge
  def hoge
    bulk_puts_eval binding, <<-EOS
__callee__
__method__
    EOS
  end

  alias_method :hoge_alias, :hoge
end

# top level
bulk_puts_eval binding, <<-EOS
__callee__.nil?
__method__.nil?
EOS

Hoge.new.hoge
Hoge.new.hoge_alias

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils
出力
__callee__.nil? # => true
__method__.nil? # => true
__callee__ # => :hoge
__method__ # => :hoge
__callee__ # => :hoge_alias
__method__ # => :hoge