42

Alias in Ruby

 5 years ago
source link: https://www.tuicool.com/articles/hit/UVbaMf3
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
ny6nIbE.png!web

Alias in Ruby

In the following article, we’re going to explore the following topics:

alias
alias_method

Feel free to have a look to my new eBook: :book: RUBY OBJECT MODEL :book::gem:

The alias method

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:

:fullname
User#fullname
User

Now, let’s have a look to the new entry for the username alias. This entry contains:

:username
User#fullname
User

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

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

:gem::gem:Here’s a link to my new ebook project :book: Ruby Object Model :book: !! :gem::gem:

Feel free to :clap: and share this article if it has been useful for you. :rocket:

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK