Should I use alias or alias_method?

后端 未结 7 1757
再見小時候
再見小時候 2020-11-30 16:25

I found a blog post on alias vs. alias_method. As shown in the example given in that blog post, I simply want to alias a method to another within t

7条回答
  •  清歌不尽
    2020-11-30 16:51

    alias_method can be redefined if need be. (it's defined in the Module class.)

    alias's behavior changes depending on its scope and can be quite unpredictable at times.

    Verdict: Use alias_method - it gives you a ton more flexibility.

    Usage:

    def foo
      "foo"
    end
    
    alias_method :baz, :foo
    

提交回复
热议问题