ZetCode

Ruby alias Keyword

last modified October 18, 2026

This tutorial explains how to use Ruby's alias keyword to create method aliases. Method aliasing provides alternative names for existing methods.

The alias keyword creates a new name for an existing method. The original method remains accessible. An alias is a copy of the method as it exists when the alias is defined, so a later redefinition of the original does not change the alias.

Aliasing is useful for backward compatibility, readability, or method augmentation. It works with instance methods and, inside a singleton class, with class methods. Aliases can be defined inside classes or modules. An alias also keeps the visibility of the original method, so aliasing a private method produces another private method.

Basic Method Aliasing

This example shows the simplest use of alias to create an alternative method name. Both names refer to the same implementation.

basic_alias.rb
class Greeter
  def hello
    puts "Hello!"
  end
  
  alias hi hello
end

g = Greeter.new
g.hello  # prints "Hello!"
g.hi     # prints "Hello!"

The hi method becomes an exact copy of hello. Both methods can be called interchangeably. The alias is available to all instances.

Aliasing Built-in Methods

Ruby allows aliasing built-in methods. This example creates a more readable name for the puts method.

builtin_alias.rb
alias say puts

say "This works like puts"
say 1, 2, 3

The say alias works exactly like puts, accepting multiple arguments. At the top level, the alias becomes a private method on Object, so it can be called without a receiver anywhere in the program. It is not a public method of every object.

Alias in Module Context

When used inside modules, alias affects all classes that include the module. This demonstrates module method aliasing.

module_alias.rb
module Logging
  def log(message)
    puts "[LOG] #{message}"
  end
  
  alias record log
end

class Service
  include Logging
end

s = Service.new
s.log("Starting service")    # prints "[LOG] Starting service"
s.record("Service started")  # prints "[LOG] Service started"

Both log and record methods are available to any class that includes the Logging module. The alias works at the module level.

Method Augmentation with Alias

A common pattern uses alias to preserve original methods when augmenting behavior. This example adds logging to an existing method.

augmentation.rb
class Calculator
  def add(x, y)
    x + y
  end
end

class EnhancedCalculator < Calculator
  alias original_add add
  
  def add(x, y)
    puts "Adding #{x} and #{y}"
    original_add(x, y)
  end
end

calc = EnhancedCalculator.new
puts calc.add(3, 4)  # prints "Adding 3 and 4" and 7

The original add method is preserved as original_add. The new version adds logging while maintaining the same interface. The alias keeps the original implementation even after add is redefined.

Alias with Method Parameters

Method aliases preserve all original parameters. This example shows aliasing methods with different parameter patterns.

parameters.rb
class Formatter
  def format_name(first, last)
    "#{first} #{last}"
  end
  
  alias full_name format_name
  
  def format_date(year, month=1, day=1)
    "#{year}-#{month}-#{day}"
  end
  
  alias date format_date
end

f = Formatter.new
puts f.full_name("John", "Doe")  # prints "John Doe"
puts f.date(2025)                # prints "2025-1-1"

Both required and optional parameters work correctly with aliases. The alias maintains the exact same signature as the original method.

Dynamic Aliasing

Ruby allows creating aliases dynamically using alias_method. This method accepts symbols or strings, including names stored in variables, and returns the new method name. The alias keyword is a keyword with its own syntax; it is normally used with literal method names.

This example creates aliases from a class method at runtime.

dynamic.rb
class DynamicAlias
  def greet
    puts "Hello!"
  end
  
  def self.create_alias(new_name)
    alias_method new_name, :greet
  end
end

DynamicAlias.create_alias(:welcome)
DynamicAlias.create_alias(:salute)

obj = DynamicAlias.new
obj.welcome  # prints "Hello!"
obj.salute   # prints "Hello!"

The create_alias class method generates new aliases at runtime. This technique is useful for metaprogramming scenarios. Like the original method, the generated aliases are public here.

Alias Chains and Inheritance

Aliases work with inherited methods. This example demonstrates method chaining through aliases instead of super.

inheritance.rb
class Parent
  def process
    puts "Parent processing"
  end
end

class Child < Parent
  alias parent_process process
  
  def process
    puts "Child preprocessing"
    parent_process
    puts "Child postprocessing"
  end
end

Child.new.process
# Output:
# Child preprocessing
# Parent processing
# Child postprocessing

The output shows the complete call chain: Child's preprocessing, Parent's processing, and Child's postprocessing. The alias copies the inherited method before the child overrides it, so the parent implementation remains reachable.

Source

Ruby Keywords Documentation

This tutorial covered Ruby's alias keyword with examples showing basic usage, method augmentation, dynamic aliasing, and inheritance patterns.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Ruby tutorials.