What is the difference between an alias method and an alias character?

I read some alias method as follows:

alias :original_method :method 

It looks like ruby ​​symbols to me. What difference does it make if I typed such an alias:

 alias original_method method 

Will the result be different?

+6
source share
2 answers

There is no difference in the documentation :

The alias keyword is most often used for aliases. when smoothing a method, you can use either its name or symbol:

 alias new_name old_name alias :new_name :old_name 
+7
source

Besides the fact that one of them is a method (and therefore evaluates its arguments like any method), you should leave the general use cases to find the difference.

For example, if you do

 class Foo def old_name 'foo' end def self.rename_name alias_method :new_name, :old_name end end class Bar < Foo def new_name 'bar' end end Bar.rename_name 

Then Bar overwrites its new_name method, and Foo is untouched

However, if you change this to use alias ie

 class Foo def old_name 'foo' end def self.rename_name alias :new_name :old_name end end class Bar < Foo def new_name 'bar' end end Bar.rename_name 

Then the Bar class does not change, and Foo gets a method called: new_name. This is not like the difference between using define_method and defining a method with def .

In one case, the scope is purely lexical: the location of the line that calls alias completely determines what happens, while in the other case it is self when ruby ​​evaluates this bit of code.

+1
source

Source: https://habr.com/ru/post/975427/


All Articles