LoginSignup
14
10

More than 5 years have passed since last update.

[Ruby]__method__と__callee__

Last updated at Posted at 2016-09-27

__method____callee__も現在のメソッド名をシンボルで返すのだが、

def method
  __method__
end
#=> method

def callee
  __callee__
end
#=> callee

alias_methodの時のみ、違いが出る。

class Foo
  def old_method
    __method__
  end

  def old_callee
    __callee__
  end

  alias_method :new_method, :old_method
  alias_method :new_callee, :old_callee
end

Foo.new.new_method
#=> :old_method
Foo.new.new_callee
#=> :new_callee

__method__はaliasの元のmethod名が返され、
__callee__はaliasのmethod名が返される。

alias_methodでなく、aliasの時も、

def old_method
  __method__
end
def old_callee
  __callee__
end
alias :new_method :old_method
alias :new_callee :old_callee

new_method
#=> :old_method
new_callee
#=> :new_callee
14
10
0

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
14
10