我找到了一篇关于alias vs. alias_method的博客文章。正如那篇博客文章中给出的例子所示,我只是想在同一个类中将一个方法别名为另一个方法。我应该用哪一种?我总是看到alias使用,但有人告诉我alias_method更好。
别名的使用
class User
def full_name
puts "Johnnie Walker"
end
alias name full_name
end
User.new.name #=>Johnnie Walker
alias_method的使用
class User
def full_name
puts "Johnnie Walker"
end
alias_method :name, :full_name
end
User.new.name #=>Johnnie Walker
博客文章链接此处
除了语法之外,主要的区别在于作用域:
# 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'
在上面的例子中,方法“name”选择在“Developer”类中定义的方法“full_name”。现在让我们试试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'
使用别名时,方法“name”不能选择Developer中定义的方法“full_name”。
这是因为alias是一个关键字,并且它是词法范围的。这意味着它将self视为读取源代码时self的值。相反,alias_method将self视为在运行时确定的值。
来源:http://blog.bigbinary.com/2012/01/08/alias-vs-alias-method.html
Alias_method new_method, old_method
Old_method将在一个类或模块中声明,这个类或模块现在被继承到我们的类中,new_method将被使用。
可以是变量,也可以是方法。
假设Class_1有old_method, Class_2和Class_3都继承了Class_1。
如果Class_2和Class_3的初始化是在Class_1中完成的,那么两者在Class_2和Class_3中可以有不同的名称及其用法。
在提出这个问题一年后,一篇关于这个主题的新文章出现了:
http://erniemiller.org/2014/10/23/in-defense-of-alias/
似乎“这么多人,这么多人。”前一篇文章作者鼓励使用alias_method,后一篇文章建议使用alias。
然而,在上面的博文和回答中,这些方法都有一个共同的概述:
当您希望将别名限制在定义的范围内时,请使用别名
使用alias_method允许继承类访问它