Question

Ruby alias vs alias_method: When to Use Each

rubyalias

Question

I want to create another name for a method inside the same Ruby class. I found examples using both alias and alias_method, and I am not sure which one I should use.

For example, both of these seem to work:

class User
  def full_name
    puts "Johnnie Walker"
  end

  alias name full_name
end

User.new.name
# => Johnnie Walker
class User
  def full_name
    puts "Johnnie Walker"
  end

  alias_method :name, :full_name
end

User.new.name
# => Johnnie Walker

Which one should I use when aliasing a method in Ruby, and what is the practical difference between alias and alias_method?

Short Answer

By the end of this page, you will understand how method aliasing works in Ruby, the difference between alias and alias_method, when each one is appropriate, and why alias_method behaves more like a normal method call while alias is a Ruby keyword with slightly different rules.

Concept

In Ruby, aliasing a method means giving an existing method another name. After aliasing, both names point to the same implementation as it existed at the time the alias was created.

This is useful when you want to:

  • provide a shorter or clearer method name
  • keep backward compatibility after renaming a method
  • wrap or override a method while still being able to call the original version
  • support library code that expects a different method name

The two common ways to do this are:

alias new_name old_name

and

alias_method :new_name, :old_name

Although they look similar, they are not exactly the same:

  • alias is a keyword in Ruby
  • alias_method is a method defined on Module

That distinction affects how and where they work.

The most important practical idea

If you are writing normal class code and simply want to create another method name, both often work.

However:

  • alias has special syntax and works at parse time as a keyword
  • alias_method is called at runtime like a regular method
  • alias_method is often preferred in metaprogramming or when method names are stored in variables or built dynamically

Why this matters

In real Ruby codebases, developers often alias methods during:

  • refactoring old APIs
  • monkey patching
  • decorating methods
  • Rails-style metaprogramming

Knowing the difference helps you choose the right tool and avoid surprising behavior.

Mental Model

Think of a method as a person, and the method name as that person's label.

Aliasing gives the same person another label.

If full_name is the original label, then name becomes a second label for the same method body.

  • alias is like writing a second label directly on the box using Ruby's built-in language syntax.
  • alias_method is like calling a helper function that adds the new label for you.

Both can create the same result, but they work differently behind the scenes.

Another useful way to think about it:

  • alias is part of Ruby's grammar
  • alias_method is part of Ruby's object model

That is why alias_method fits better with dynamic code.

Take Quiz

Syntax and Examples

Basic syntax

Using alias:

class User
  def full_name
    "Johnnie Walker"
  end

  alias name full_name
end

user = User.new
puts user.name       # Johnnie Walker
puts user.full_name  # Johnnie Walker

Using alias_method:

class User
  def full_name
    "Johnnie Walker"
  end

  alias_method :name, :full_name
end

user = User.new
puts user.name       # Johnnie Walker
puts user.full_name  # Johnnie Walker

What both examples do

Both create a second method name, name, that calls the same method body as full_name.

A more realistic example

class Product
  def price_in_cents
    1999
  end

  alias_method :price, :price_in_cents
end

product = Product.new
puts product.price           # 1999
puts product.price_in_cents  # 1999

This can be useful when:

  • an old method name still needs to work
  • a new public API should be simpler

Dynamic method names

This is where alias_method becomes more useful:

class User
  def full_name
    "Johnnie Walker"
  end

  new_name = :name
  old_name = :full_name

  alias_method new_name, old_name
end

You cannot use variables with alias in the same way, because alias expects method names in its special keyword syntax.

Step by Step Execution

Consider this example:

class User
  def full_name
    "Johnnie Walker"
  end

  alias_method :name, :full_name
end

user = User.new
puts user.name

Step by step

  1. Ruby starts reading the User class.
  2. It defines the instance method full_name.
  3. Ruby executes alias_method :name, :full_name while building the class.
  4. A new method name, name, is added as an alias of full_name.
  5. User.new creates a new User object.
  6. user.name is called.
  7. Ruby looks for a method named name.
  8. It finds that name is an alias for full_name.
  9. The original method body runs and returns "Johnnie Walker".
  10. puts prints the returned string.

Important behavior: aliasing copies the current method reference

class User
  def full_name
    "Old Name"
  end

  alias_method :name, :full_name

  def full_name
    "New Name"
  end
end

user = User.new
puts user.full_name # New Name
puts user.name      # Old Name

When the alias was created, name pointed to the original implementation. Redefining full_name later does not automatically update the aliased method.

Real World Use Cases

1. Keeping old APIs working

class Account
  def email_address
    "user@example.com"
  end

  alias_method :email, :email_address
end

This is common during refactoring when older code still calls email.

2. Providing a clearer public name

class Order
  def total_in_cents
    2500
  end

  alias_method :total, :total_in_cents
end

The internal method may be explicit, while the public API stays simple.

3. Wrapping an existing method

class LoggerService
  def log(message)
    puts message
  end

  alias_method :original_log, :log

  def log(message)
    original_log("[LOG] #{message}")
  end
end

This pattern lets you preserve the old behavior before redefining the method.

4. Compatibility in libraries

A gem may expose two names for the same behavior to support older versions of its API.

5. Framework metaprogramming

Ruby frameworks and DSL-heavy code often generate or alias methods dynamically, especially with alias_method.

Take Quiz

Real Codebase Usage

In real projects, developers usually choose based on context.

Common pattern: preserving the original method before overriding

class PaymentService
  def process
    "original processing"
  end

  alias_method :process_without_logging, :process

  def process
    puts "Starting..."
    result = process_without_logging
    puts "Done"
    result
  end
end

This pattern appears in decorators, monkey patches, and legacy Rails code.

Dynamic aliasing in metaprogramming

class Report
  def self.create_alias(new_name, old_name)
    alias_method new_name, old_name
  end

  def generate
    "report"
  end

  create_alias :build, :generate
end

Because alias_method is a normal method call, it works well when names come from variables or helper methods.

Guarding public APIs during refactors

Teams often rename methods but keep aliases temporarily so old callers do not break.

Error handling and deprecation strategy

Sometimes an old method remains as an alias while logs or warnings are added elsewhere before the method is eventually removed.

Practical rule in many codebases

  • Use alias for simple, static aliasing inside a class body.
  • Use alias_method when doing anything dynamic, reflective, or metaprogrammed.

Common Mistakes

1. Thinking the alias updates automatically after redefining the original method

Broken expectation:

class User
  def full_name
    "Old Name"
  end

  alias_method :name, :full_name

  def full_name
    "New Name"
  end
end

puts User.new.name
# Many beginners expect: "New Name"
# Actual result: "Old Name"

How to avoid it:

  • Create the alias after the final method definition if you want the alias to point to the latest implementation.

2. Using alias with variables

Broken code:

class User
  def full_name
    "Johnnie Walker"
  end

  new_name = :name
  old_name = :full_name

  alias new_name old_name
end

This does not work the way many beginners expect because alias uses special syntax, not normal argument passing.

Use this instead:

alias_method new_name, old_name

3. Forgetting symbol syntax with alias_method

Broken code:

alias_method "name", "full_name"

While strings may be converted in some Ruby contexts, the normal and idiomatic form is:

alias_method :name, :full_name

4. Calling alias_method where it is not available

alias_method is defined on Module, so it is intended for class or module definitions.

5. Confusing method aliasing with variable assignment

This is not aliasing a method:

name = full_name

That would attempt to call full_name and assign its result to a variable.

6. Using puts when you really want a return value

Your original example uses:

def full_name
  puts "Johnnie Walker"
end

That prints text, but the method returns nil because puts returns nil.

In many cases, this is better:

def full_name
  "Johnnie Walker"
end

Then the caller can decide whether to print it.

Comparisons

Featurealiasalias_method
TypeRuby keywordMethod on Module
Syntax styleSpecial language syntaxNormal method call
Accepts dynamic names from variablesNoYes
Common for simple static aliasingYesYes
Common in metaprogrammingLess convenientMore convenient
Can be used with symbolsUses bare names or symbols in keyword formTypically uses symbols

alias vs alias_method in practice

  • Choose alias when the names are fixed and you want concise syntax.
  • Choose alias_method when names are dynamic or you are writing metaprogramming-heavy code.

Related comparison: aliasing vs redefining

TechniqueWhat it does
AliasingGives an existing method another name
RedefiningReplaces a method with a new implementation

Related comparison: aliasing vs forwarding

TechniqueExample ideaUse case
Aliasingalias_method :name, :full_nameSame behavior under another name
Forwardingdef name; full_name; endAdd logic later or customize behavior

Forwarding can be clearer if you want the new method to remain tied to the current version of the original method.

Take Quiz

Cheat Sheet

Quick rules

  • alias is a Ruby keyword
  • alias_method is a method on Module
  • Both create another name for an existing method
  • The alias points to the method implementation as it existed when the alias was created
  • Use alias_method for dynamic names or metaprogramming
  • Use either for simple class-level aliasing, depending on team style

Syntax

alias new_name old_name
alias_method :new_name, :old_name

Static example

class User
  def full_name
    "Johnnie Walker"
  end

  alias name full_name
end

Dynamic example

class User
  def full_name
    "Johnnie Walker"
  end

  new_name = :name
  old_name = :full_name
  alias_method new_name, old_name
end

Important edge case

class Example
  def greet
    "old"
  end

  alias_method :hello, :greet

  def greet
    "new"
  end
end
  • hello returns "old"
  • greet returns "new"

Good default

  • Simple static alias: alias is fine
  • Dynamic or metaprogrammed alias: use alias_method

FAQ

Should I use alias or alias_method in Ruby?

For simple aliasing with fixed method names, either can work. alias_method is often preferred when method names are dynamic or when working with metaprogramming.

Is alias_method better than alias?

Not always. It is not universally better. It is just more flexible because it behaves like a normal method call.

Does an alias follow later changes to the original method?

No. The alias points to the implementation that existed when the alias was created.

Can I use variables with alias?

No, not in the same way as alias_method. alias uses special keyword syntax and is not designed for dynamic arguments.

Why do many Ruby examples use alias?

Because it is short, built into the language, and works well for straightforward class definitions.

Why do frameworks often use alias_method?

Because frameworks frequently generate behavior dynamically, and alias_method works better with variables, helper methods, and metaprogramming patterns.

Is def name; full_name; end the same as aliasing?

No. That creates a forwarding method, not an alias. A forwarding method calls the current version of full_name, while an alias captures the method implementation at alias time.

Take Quiz

Mini Project

Description

Build a small Ruby class that exposes both a new and an old method name for the same behavior. This demonstrates how aliasing helps during refactoring and backward compatibility.

Goal

Create a class where an old API method name still works after introducing a clearer new method name.

Requirements

  • Create a Ruby class with one original instance method
  • Add a second method name that aliases the original one
  • Show that both method names return the same result
  • Redefine the original method after aliasing and observe the difference
  • Print the results so the behavior is easy to compare
Take Quiz

Keep learning

Related questions

View more