Alias in Ruby
an overview of alias, alias_method and scope aliases
In the following article, we’re going to explore the following topics:
- the
alias
keyword - the
alias_method
method - aliases and scopes
- aliases behind the scene
The alias keyword
Ruby provides an alias
keyword to deal with method and attribute aliases
Here we define a User#fullname
method and we define a username
alias for this method.
Then, the username
alias is aliased with a name
alias.
So, a call to name
or username
will call the code defined within the User#fullname
method and return the same result.
Note that it’s possible to define an alias for an existing alias.
The alias_method
method
The Module#alias_method
method shares the same behavior as the alias
keyword but it complies to the method syntax
Like the alias
keyword, we define a User#fullname
method and we define a username
alias for this method.
Then the username
alias is aliased with a name
alias.
So, a call to name
, username
or fullname
returns the same result.
We can see that the alias_method
method takes a String
or a Symbol
as argument that allows Ruby to identify the alias and the method to alias.
If alias
and Module#alias_method
share the same behavior, then what’s the purpose of dealing with two things that do the exact same job?
The answer is that they don’t exactly do the same thing.
Aliases and scopes
In effect, the Module#alias_method
acts differently than the alias
keyword on one specific point: the scope.
Let’s have a look to this example
Here we can see that the call to alias_method
within the Device#alias_description
method defines the describe
alias on the Microwave#description
method and not on the Device#description
one.
Now let’s see what happens with the alias
keyword
Here we can see that the call to alias
within the Device#alias_description
method sets the describe
alias on the Device#description
method and not on the Microwave#description
one.
Aliases behind the scene
Let’s get back to the User
class example to figure out what happens when an alias is defined
Behind the scene the username
alias is treated as a method.
In effect, in Ruby each method is inserted in a table that keeps track of all the methods of your program. This table is called the method_entry
table.
So, for the fullname
method, a new entry is inserted in the method_entry
table. This entry contains the following informations:
- the
:fullname
method identifier - the content of the
User#fullname
method - the
User
class
Now, let’s have a look to the new entry for the username
alias. This entry contains:
- the
:username
method identifier - The content of the
User#fullname
method - the
User
class
That’s how the alias
keyword and the alias_method
method are able to define an alias for an existing method.
Note that an entry contains a way more informations than what I’ve described. But let’s keep it simple to focus on aliases.
Voilà!
RubyCademy is now available on Youtube! ▶️ 🚀 🤩 💎
We publish short videos (maximum 5 minutes) that talk about technical notions, quick wins and tools (..and a couple of geek stuffs 😅).
Feel free to click on the image below to access our Youtube channel