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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…