Should I use alias or alias_method?
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 the same class. Which should I use? I always see alias
used, but someone told me alias_method
is better.
Usage of alias
class User
def full_name
puts "Johnnie Walker"
end
alias name full_name
end
User.new.name #=>Johnnie Walker
Usage of alias_method
class User
def full_name
puts "Johnnie Walker"
end
alias_method :name, :full_name
end
User.new.name #=>Johnnie Walker
Blog post link here
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
Apart from the syntax, the main difference is in the scoping:
# scoping with alias_method
class User
def full_name
puts "Johnnie Walker"
end
def self.add_rename
alias_method :name, :full_name
end
end
class Developer < User
def full_name
puts "Geeky geek"
end
add_rename
end
Developer.new.name #=> 'Geeky geek'
In the above case method “name” picks the method “full_name” defined in “Developer” class. Now lets try with alias
.
class User
def full_name
puts "Johnnie Walker"
end
def self.add_rename
alias name full_name
end
end
class Developer < User
def full_name
puts "Geeky geek"
end
add_rename
end
Developer.new.name #=> 'Johnnie Walker'
With the usage of alias the method “name” is not able to pick the method “full_name” defined in Developer.
This is because alias
is a keyword and it is lexically scoped. It means it treats self
as the value of self at the time the source code was read . In contrast alias_method
treats self
as the value determined at the run time.
Source: http://blog.bigbinary.com/2012/01/08/alias-vs-alias-method.html
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
.
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
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