Should I use alias or alias_method?

后端 未结 7 1679
再見小時候
再見小時候 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:49

    A year after asking the question comes a new article on the subject:

    http://erniemiller.org/2014/10/23/in-defense-of-alias/

    It seems that "so many men, so many minds." From the former article author encourages to use alias_method, while the latter suggests using alias.

    However there's a common overview of these methods in both blogposts and answers above:

    • use alias when you want to limit aliasing to the scope where it's defined
    • use alias_method to allow inherited classes to access it
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-30 16:58

    I think there is an unwritten rule (something like a convention) that says to use 'alias' just for registering a method-name alias, means if you like to give the user of your code one method with more than one name:

    class Engine
      def start
        #code goes here
      end
      alias run start
    end
    

    If you need to extend your code, use the ruby meta alternative.

    class Engine
      def start
        puts "start me"
      end
    end
    
    Engine.new.start() # => start me
    
    Engine.class_eval do
      unless method_defined?(:run)
        alias_method :run, :start
        define_method(:start) do
          puts "'before' extension"
          run()
          puts "'after' extension"
        end
      end
    end
    
    Engine.new.start
    # => 'before' extension
    # => start me
    # => 'after' extension
    
    Engine.new.run # => start me
    
    0 讨论(0)
  • 2020-11-30 16:59

    alias_method new_method, old_method

    old_method will be declared in a class or module which is being inherited now to our class where new_method will be used.

    these can be variable or method both.

    Suppose Class_1 has old_method, and Class_2 and Class_3 both of them inherit Class_1.

    If, initialization of Class_2 and Class_3 is done in Class_1 then both can have different name in Class_2 and Class_3 and its usage.

    0 讨论(0)
  • 2020-11-30 17:01

    A point in favor of alias instead of alias_method is that its semantic is recognized by rdoc, leading to neat cross references in the generated documentation, while rdoc completely ignore alias_method.

    0 讨论(0)
  • 2020-11-30 17:04

    The rubocop gem contributors propose in their Ruby Style Guide:

    Prefer alias when aliasing methods in lexical class scope as the resolution of self in this context is also lexical, and it communicates clearly to the user that the indirection of your alias will not be altered at runtime or by any subclass unless made explicit.

    class Westerner
      def first_name
       @names.first
      end
    
     alias given_name first_name
    end
    

    Always use alias_method when aliasing methods of modules, classes, or singleton classes at runtime, as the lexical scope of alias leads to unpredictability in these cases

    module Mononymous
      def self.included(other)
        other.class_eval { alias_method :full_name, :given_name }
      end
    end
    
    class Sting < Westerner
      include Mononymous
    end
    
    0 讨论(0)
提交回复
热议问题