You are reading the article How Does Alias Statement Works In Ruby With Examples updated in October 2023 on the website Saigonspaclinic.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 How Does Alias Statement Works In Ruby With Examples
Introduction to Ruby aliasRuby allows developer to define or rename for some specific purposes, here renaming means the older method behavior with new name. Basically alias is a keyword in the Ruby which takes two parameters one is the older name and another one is the new names, we use these things mostly when we feel there are some ambiguity inside the code for naming of the methods, this alias keyword will be more useful where we have too many method with similar names as we can rename those functions according to our understanding and according to the purpose of the methods which make it more understandable for other developers.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
There could be two important syntaxes for it. We just need to mention the new name and old name for alias and a new name for the method will be created and we can access it with the new change name.
alias oldnamenewname alias :oldname :newname How does alias Statement works in Ruby?Given below is the working of alias statement in Ruby:
Once we use keyword alias ruby expect two attributes one is the older name and another one is the new name which we want to assign.
Outside the class object will have the changed name and at any instance we can access the method with the object of the class.
Note: Its best use is, when some other class or library which we are including in our class contains some methods with the same name which our class contains, at that time we can rename our methods for better understanding.
Examples of Ruby aliasGiven below are the examples mentioned:
Example #1In the below example we have created a Student class and inside the Student class we have created a method called studentName which is printing the name. Inside the same class we are renaming the methods with various new other methods names and we are using the changed names outside of the object of the Student class and getting the same outputs.
Defining a class and inside the class we are making alias of the names of the several functions for uses, and these functions will be available outside with the name of the alias we given.
Code
class Student defstudentName 'Ranjan Kumar pandey' end #Creation of alias for methods simply renaming the method alias student studentName alias name student alias identity name end s = Student.new #Here we are calling the methods with the name which we have given for it as alias puts s.studentName puts s.name puts s.identityOutput:
Example #2In the below example we have created a Man class and inside the Man class we have created a method called studentName which is printing the name. Inside the same class we are renaming the method and using the changed name outside of the object of the Student class.
Code:
class Man def talk "Hello, I can talk" end #Creation of alias for methods simply renaming the method alias_method "#{name.downcase}_talk", :talk end #Here we are calling the methods with the name which we have given for it as alias p Man.new.man_talkOutput:
Example #3In the below example we have created a Human class and inside the Human class we have created some methods for different types of humans and these methods are printing their details. Inside the same class we are renaming the method and using the changed name outside of the object of the Student class.
Defining a class and inside the class we are making alias of the names of the several functions for uses, and these functions will be available outside with the name of the alias we given.
Code:
class Human def boy puts 'Hello, I am man' end def girl puts 'Hello ,I am girl' end def child puts 'Hello ,I am child' end #Creation of alias for methods simply renaming the method alias :boyMessege :boy alias :girlsMessege :girl alias :childMessege :child end h = Human.new #Here we are calling the methods with the name which we have given for it as alias puts h.boyMessege puts h.girlsMessege puts h.childMessegeOutput:
Example #4Below is the example where we have created a class called Cal and inside this class we are doing some arithmetic calculation, here inside the same class we are renaming the names of the class. We can see on the outside we are accessing the changed name of methods with exact same operations. Many times it is a good idea to rename a very big method for calculation which reduces the code size as well as it reduces the typing.
Defining a class and inside the class we are making alias of the names of the several functions for uses, and these functions will be available outside with the name of the alias we given.
Code:
class Cal def addition(a,b) puts "Hello ,The addition of two number is #{+b}" end def multiplication(a,b) puts "Hello ,The multiplication of two number is #{a*b}" end defsubstract(a,b) puts "Hello ,Thesubstract of two number is #{b-a}" end def division(a,b) puts "Hello ,The division of two number is #{b/a}" end alias :add :addition alias :mul :multiplication #Creation of alias for methods simply renaming the method alias :subs :substract alias :div :division end c = Cal.new #Here we are calling the methods with the name which we have given for it as alias puts c.add(2,4) puts c.mul(2,4) puts c.subs(2,4) puts c.div(2,4)Output:
ConclusionFrom these tutorials we learned about the basics of the alias in Ruby, we also learned about the working of the alias, we came to know that we can use alias where we have many libraries and other methods to create misunderstanding then we rename the method for our own understanding.
Recommended Articles
We hope that this EDUCBA information on “Ruby alias” was beneficial to you. You can view EDUCBA’s recommended articles for more information.
Ruby Modules
Ruby Array Methods
While Loop in Ruby
Ruby Operators
You're reading How Does Alias Statement Works In Ruby With Examples
Update the detailed information about How Does Alias Statement Works In Ruby With Examples on the Saigonspaclinic.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!