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 with 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 an 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 at 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 piece of information:
- the
:fullname
method identifier - the content of the
User#fullname
method - the
User
class
Now, let’s have a look at 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 alias
and alias_method
are able to define an alias for an existing method.
Note that an entry contains way more information than what I’ve described. But let’s keep it simple to focus on aliases.
Ruby Mastery
We’re currently finalizing our first online course: Ruby Mastery.
Join the list for an exclusive release alert! 🔔
Also, you can follow us on x.com as we’re very active on this platform. Indeed, we post elaborate code examples every day.
💚