Science, Tech, Math › Computer Science Aliasing a Method in Ruby Share Flipboard Email Print geralt/Pixabay Computer Science Ruby Programming PHP Programming Perl Python Java Programming Javascript Programming Delphi Programming C & C++ Programming Visual Basic View More By Michael Morin Michael Morin Computer Science Expert Michael Morin is a computer programmer specializing in Linux and Ruby. He has 30 years of experience studying, teaching and using the programming language. Learn about our Editorial Process Updated on March 07, 2019 To alias a method or variable name in Ruby is to create a second name for the method or variable. Aliasing can be used either to provide more expressive options to the programmer using the class or to help override methods and change the behavior of the class or object. Ruby provides this functionality with the "alias" and "alias_method" keywords. Create a Second Name The alias keyword takes two arguments: the old method name and the new method name. The method names should be passed as labels, as opposed to strings. Labels are used to refer to methods and variables without directly referencing them. If you're a new Ruby programmer, the concept of labels may seem odd, but whenever you see a label like ":methodname," just read it as "the thing called methodname." The following example declares a new class and creates an alias for the on method called start. #!/usr/bin/env rubyclass Microwavedef onputs "The microwave is on"endalias :start :onendm = Microwave.newm.start # same as m.on Change the Behavior of a Class There may be times when you want to change the behavior of a class after it's been declared. You can alias and add new methods to an existing class by creating second class declaration that has the same name as the existing class declaration. You can also add aliases and methods to individual objects using a syntax similar to the inherited class syntax. The behavior of any class can be changed by creating an alias for any method and then creating a new method (with the original method name) that calls the method with the alias. In the following example, a microwave class is declared and an instance is created. The second class declaration uses the alias method to change the behavior of the "on" method in order to add a warning message. The third class declaration is used to change the behavior of the specific microwave instance to add an even more stern warning. When aliasing a method multiple times, be sure to use different method names to store the old method. #!/usr/bin/env rubyclass Microwavedef on puts "Microwave is on" end endm = Microwave.newm.onclass Microwave alias :old_on1 :ondef on puts "Warning: Do not insert metal objects!" old_on1 end endm.on# Message for this specific microwaveclass < def onputs "This microwave is weak, add extra time"old_on2endendm.on # Displays extra messagem2 = Microwave.newm2.on # Does not display extra message Cite this Article Format mla apa chicago Your Citation Morin, Michael. "Aliasing a Method in Ruby." ThoughtCo, Aug. 28, 2020, thoughtco.com/aliasing-in-ruby-2908190. Morin, Michael. (2020, August 28). Aliasing a Method in Ruby. Retrieved from https://www.thoughtco.com/aliasing-in-ruby-2908190 Morin, Michael. "Aliasing a Method in Ruby." ThoughtCo. https://www.thoughtco.com/aliasing-in-ruby-2908190 (accessed June 8, 2023). copy citation