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:
aliasis a keyword in Rubyalias_methodis a method defined onModule
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:
aliashas special syntax and works at parse time as a keywordalias_methodis called at runtime like a regular method
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.
aliasis like writing a second label directly on the box using Ruby's built-in language syntax.alias_methodis 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:
aliasis part of Ruby's grammaralias_methodis part of Ruby's object model
That is why alias_method fits better with dynamic code.
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
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
- Ruby starts reading the
Userclass. - It defines the instance method
full_name. - Ruby executes
alias_method :name, :full_namewhile building the class. - A new method name,
name, is added as an alias offull_name. User.newcreates a newUserobject.user.nameis called.- Ruby looks for a method named
name. - It finds that
nameis an alias forfull_name. - The original method body runs and returns
"Johnnie Walker". - prints the returned string.
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()
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
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.
Comparisons
| Feature | alias | alias_method |
|---|---|---|
| Type | Ruby keyword | Method on Module |
| Syntax style | Special language syntax | Normal method call |
| Accepts dynamic names from variables | No | Yes |
| Common for simple static aliasing | Yes | Yes |
| Common in metaprogramming | Less convenient | More convenient |
| Can be used with symbols | Uses bare names or symbols in keyword form | Typically uses symbols |
alias vs alias_method in practice
Cheat Sheet
Quick rules
aliasis a Ruby keywordalias_methodis a method onModule- 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_methodfor 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"
new_name =
old_name =
alias_method new_name, old_name
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 the same as aliasing?
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
Keep learning
Related questions
Calling a Class Method from an Instance in Ruby
Learn how to call a class method from an instance in Ruby using self.class, with examples, pitfalls, and practical usage patterns.
Calling an Overridden Monkey-Patched Method in Ruby
Learn how to call the original method when monkey patching in Ruby, including alias_method patterns, examples, pitfalls, and practical usage.
Convert a Unix Timestamp to Ruby DateTime
Learn how to convert Unix timestamps to Ruby DateTime and Time objects, with examples, differences, pitfalls, and practical Ruby usage.