我发现了一篇关于alias
与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
如果需要,可以重新定义alias_method
。 (它在Module
类中定义。)
alias
的行为根据其范围而变化,有时可能非常难以预测。
结论:使用alias_method
- 它给你更多的灵活性。
用法:
def foo
"foo"
end
alias_method :baz, :foo
除语法外,主要区别在于范围界定:
# 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
我认为有一个不成文的规则(类似于约定),只是为了注册方法名别名而使用“别名”,意味着如果你想给你的代码用户一个带有多个名字的方法:
class Engine
def start
#code goes here
end
alias run start
end
如果您需要扩展代码,请使用ruby元替代方案。
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
而不是alias_method
的观点是它的语义被rdoc识别,导致生成的文档中的整齐交叉引用,而rdoc完全忽略alias_method
。
询问问题一年后,有关该主题的新文章:
http://erniemiller.org/2014/10/23/in-defense-of-alias/
似乎“这么多男人,这么多头脑。”从前一篇文章作者鼓励使用alias_method
,而后者建议使用alias
。
然而,在上面的博客文章和答案中都有这些方法的共同概述:
alias
alias_method
允许继承的类访问它现在在Ruby Style Guide处理:
在词法类作用域中使用别名方法时,首选别名,因为此上下文中self的解析也是词法,并且它清楚地向用户传达,除非明确指出,否则别名的间接不会在运行时或任何子类中更改。
class Westerner
def first_name
@names.first
end
alias given_name first_name
end
在运行时对模块,类或单例类的别名方法进行别名时始终使用alias_method,因为别名的词法范围导致这些情况下的不可预测性
module Mononymous
def self.included(other)
other.class_eval { alias_method :full_name, :given_name }
end
end
class Sting < Westerner
include Mononymous
end
alias_method new_method,old_method
old_method将在一个类或模块中声明,该类或模块现在继承到我们将使用new_method的类。
这些可以是变量或方法。
假设Class_1具有old_method,Class_2和Class_3都具有Class_1。
如果Class_1和Class_3的初始化在Class_1中完成,那么它们在Class_2和Class_3及其用法中可以具有不同的名称。
虽然它可能很小,但我喜欢别名的两件事,比alias_method更多:
(1)别名较短
和
(2)你没有打字,
我知道,这很少,但是当你经历了几百次时,你只是倾向于选择别名