Is it correct to use 'alias' or 'alias_method'?

Asked 2 years ago, Updated 2 years ago, 99 views

I'm going to use the method alias within the same class, which one should I use?

I think it's right to use alias_method just by looking at the name, but there's no problem using alias, so why did you divide two?That's what I think.

ruby alias

2022-09-21 20:57

1 Answers

First of all,

alias

Gives alias to methods or global variables. Aliases can not be defined within the method body. The aliase of the method keep the current definition of the method, even when methods are overridden. Making aliases for the numbered global variables ($1, $2,...) is prohibited. Overriding the builtin global variables may cause serious problems.

Create an alias for a method or global variable. Cannot be defined within method body. Even if the method is overridden, the alias of that method is maintained. Alias of the numbered global variable ($1, $2,...) is prohibited. Alias of embedded global variables can cause serious problems

#alias_method

Makes new_name a new copy of the method old_name. This can be used to retain access to methods that are overridden.

Create a new_name method that copies the old_name method. Overrid methods are also accessible.


2022-09-21 20:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.