2014年10月13日 星期一

[ 文章收集 ] alias vs alias_method

Source From Here
Preface
It comes up very often. Should I use alias or alias_method . Lets take a look at them in a bit detail.

alias vs alias_method
Usage of alias
  1. class User  
  2.   
  3.   def full_name  
  4.     puts "Johnnie Walker"  
  5.   end  
  6.   
  7.   alias name full_name  
  8. end  
  9.   
  10. User.new.name #=>Johnnie Walker  
Usage of alias_method
  1. class User  
  2.   
  3.   def full_name  
  4.     puts "Johnnie Walker"  
  5.   end  
  6.   
  7.   alias_method :name, :full_name  
  8. end  
  9.   
  10. User.new.name #=>Johnnie Walker  
First difference you will notice is that in case of alias_method we need to use a comma between the "new method name" and "old method name". alias_method takes both symbols and strings as input. Following code would also work:
  1. alias_method 'name''full_name'  
That was easy. Now lets take a look at how scoping impacts usage of alias and alias_method.

Scoping with alias
  1. class User  
  2.   
  3.   def full_name  
  4.     puts "Johnnie Walker"  
  5.   end  
  6.   
  7.   def self.add_rename  
  8.     alias_method :name, :full_name  
  9.   end  
  10. end  
  11.   
  12. class Developer < User  
  13.   def full_name  
  14.     puts "Geeky geek"  
  15.   end  
  16.   add_rename  
  17. end  
  18.   
  19. Developer.new.name #=> 'Gekky geek'  
In the above case method "name" picks the method "full_name" defined in "Developer" class. Now lets try with alias.
  1. class User  
  2.   
  3.   def full_name  
  4.     puts "Johnnie Walker"  
  5.   end  
  6.   
  7.   def self.add_rename  
  8.     alias :name :full_name  
  9.   end  
  10. end  
  11.   
  12. class Developer < User  
  13.   def full_name  
  14.     puts "Geeky geek"  
  15.   end  
  16.   add_rename  
  17. end  
  18.   
  19. Developer.new.name #=> 'Johnnie Walker'  
With the usage of alias the method "name" is not able to pick the method "full_name" defined in Developer.

This is because alias is a keyword and it is lexically scoped. It means it treats self as the value of self at the time the source code was read. In contrast alias_method treats self as the value determined at the run time. Overall my recommendation would be to use alias_method. Since alias_method is a method defined in class Module it can be overridden later and it offers more flexibility.

Supplement
Blog - Method alias in ruby
You can create aliases for a method and variable name in ruby. This can be helpful if you want to override the behavior of some method without changing the origin implementation of it. alias_method take a new_name as a copy name of the old_name and it has the following syntax:
  1. alias_method (new_name, old_name)  


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...