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
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:
alias
when you want to limit aliasing to the scope where it's definedalias_method
to allow inherited classes to access italias_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
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
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.
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
.
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