Difference between __callee__ and __method__

后端 未结 3 1508
挽巷
挽巷 2021-02-18 20:30

In Ruby, one can use either

__callee__ 

or

__method__ 

to find the name of the currently executing method.<

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-18 21:13

    __method__ returns defined name, and __callee__ returns called name. They are same usually, but different in a aliased method.

    def foo
    [__method__, __callee__]
    end
    alias bar foo
    p foo #=> [:foo, :foo]
    p bar #=> [:foo, :bar]
    

    link

提交回复
热议问题