我找到了一篇关于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'只是为了注册一个方法名别名,这意味着如果你想给你的代码的用户一个具有多个名称的方法:
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_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中可以有不同的名称及其用法。
除了语法之外,主要的区别在于作用域:
# 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