I found a blog post about alias alias_method. As the example in this blog post shows, I just want to alias another method in the same class. Which should I use? I always see using alias, but someone told me that alias_method is better.
Use of aliases
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
#1 building
Although it may not be big, I like aliases for two things, rather than alias_
(1) Short alias
and
(2) You don't have to type,
I know it's rare, but over time, you do it hundreds of times, and you tend to use aliases whenever possible
#2 building
I think there is an unwritten rule (similar to a convention) that says that using "aliases" is only used to register method name aliases, which means that if you want to provide code users with a method with multiple names:
class Engine def start #code goes here end alias run start end
If you need to extend the code, use the ruby meta option.
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
#3 building
A year after the question was asked, there was a new article on the subject:
http://erniemiller.org/2014/10/23/in-defense-of-alias/
It seems that "there are many people, there are many brains". Starting from the previous article, the author encourages the use of alias_method, while in the latter article, it is recommended to use alias.
However, there is a common overview of these methods in the above posts and answers:
- Use alias when you want to restrict alias to a defined scope
- Use alias method to allow inherited classes to access it
#4 building
In addition to grammar, the main differences are scope:
# 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, the method "name" selects the method "full" name "defined in the" Developer "class. Now let's try using 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'
When using aliases, method 'name' will not be able to select method 'full'name' defined in Developer.
This is because alias is a keyword and is within the lexical scope. This means that it treats self as a value of self when reading the source code. Instead, alias_method treats self as a value determined at run time.
Source: http : //blog.bigbinary.com/2012/01/08/alias-vs-alias-method.html
#5 building
If necessary, you can redefine alias method. (defined in the Module class.)
The behavior of alias varies according to its scope, sometimes it may be unpredictable.
Conclusion: using alias? Method gives you more flexibility.
Usage:
def foo "foo" end alias_method :baz, :foo