Have you used alias
in ruby yet? , so let’s find out today
1.Alias in Ruby
In this article, I will show you the following:
- The
alias
keyword - The
alias_method
keyword aliases
and scopesaliases
behind the scene
2.The alias
keyword
Ruby provides us with alias
keywork to deal with methods and attributes
1 2 3 4 5 6 7 8 9 10 11 12 | class Book def book_name "Davici code" end alias bookname book_name alias name bookname end book = Book.new p book.book_name # "Davici code" p book.bookname # "Davici code" p book.name # "Davici code" |
Here we have defined the Book#book_name
method and defined a bookname
alias for this method. So now we have bookname
is alias of book_name
method and name
is alias of bookname
Therefore, when we call name
or bookname
will return the same result.
3.The alias_method
method
1 2 3 4 5 6 7 8 9 10 11 12 | class Book def book_name "Davici code" end alias_method :bookname, :book_name alias name 'bookname', :book_name end book = Book.new p book.book_name # "Davici code" p book.bookname # "Davici code" p book.name # "Davici code" |
Like
alias
keywork we also define theBook#book_name
method and define thebookname
andname
as alias of mehtod. We easily see thatalias_method
takesString
andSymbol
as parameters, this helpsRuby
identifyalias
oralias_mehtod
4.Alias and scopes
In fact, Module#alias_method
has a different way of working from alias
keywork in a scope called scope. Consider the following example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | class Device def description 'I'm a device' end def self.alias_description alias_method :describe, :description end end class Microwave < Device def description 'I' a microwave' end alias_description end m = Microwave.new p m.description # => "I' a microwave" p m.describe # => "I' a microwave" |
Here we can see that when calling the
alisa_method
method in theDevice#alias_description
method identifies the alias defined on thedescribe
method on theMicrowave#description
method and not onDevice#description
.
Now let’s see what happens with alias
keyword:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | class Device def description 'I'm a device' end def self.alias_description alias describe description end end class Microwave < Device def description 'I' a microwave' end alias_description end m = Microwave.new p m.description # => "I' a microwave" p m.describe # => "I'm a device" |
We can see when calling alias in Device#alias_description
method, the alias describe
the Device#description
, not the Microwave#description
5.Aliases behind the scene
Let’s go back to the Book
class example to find out what happens alias is defined.
1 2 3 4 5 6 7 8 9 10 11 12 | class Book def book_name "Davici code" end alias bookname book_name alias name bookname end book = Book.new p book.book_name # "Davici code" p book.bookname # "Davici code" p book.name # "Davici code" |
Behind the scene username
alias is considered a method. In ruby, the method primer will be inserted into a table to keep track of all the methods in your program. This table is called method_entry table.
So the book_name
method is a new entry inserted into the method_entry table. Will include the following:
- Identify the method `book_name ‘
- The content of the
Book#book_name
method - Book class
That is how
alias
keyword andalias_method
can determine the existing alias method. Now we look at the new entry aliasbookname
includes:
bookname
thebookname
method- The content of the
Book#book_name
method - Class
Book
That’s how alias
keyword and alias_method
method determine aliases for an existing method. Note that the entry contains more information than I have described to keep it simple. We focus on the alias
keyword.
Draft link: https://medium.com/rubycademy/alias-in-ruby-bf89be245f69