Alias in Ruby

Mehdi Farsi
· 3 min read

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à!

ONE MORE THING ⬇

Feel free to subscribe here: www.rubycademy.com


Thank you for taking the time to read this post :-)

Feel free to 👏 and share this article if it has been useful for you. 🚀

Also, as I post an article every 3 days, feel free to follow me to be notified of my new releases.

Here is a link to my last article: redo keyword in Ruby.

rubycademy

E-Learning platform for Ruby and Ruby on Rails

Mehdi Farsi

Written by

Founder of www.rubycademy.com — I blog about Ruby & ROR - Paris

rubycademy

E-Learning platform for Ruby and Ruby on Rails

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade