Introduction
Ruby provides two ways to create method aliases: the alias keyword and the alias_method method. Both create a new name for an existing method, but they differ in syntax, scope, and flexibility. Understanding when to use each is important for clean Ruby code and metaprogramming.
alias: The Keyword
alias is a Ruby keyword that creates a copy of a method under a new name. It captures the method at the time of aliasing โ changes to the original method later do not affect the alias.
class Greeter
def hello
"Hello!"
end
alias greet hello # no commas, no colons
end
g = Greeter.new
puts g.hello # => "Hello!"
puts g.greet # => "Hello!"
Note the syntax: no parentheses, no commas, bare method names (not symbols).
alias_method: The Method
alias_method is a method defined on Module. It takes symbol or string arguments and can only be used inside a class or module body.
class A
def test1
"test"
end
alias_method :test, :test1
remove_method :test1
end
p A.singleton_methods # => []
p A.new.test # => "test"
The syntax uses symbols (or strings) and a comma:
alias_method :new_name, :old_name
Key Differences
| Feature | alias |
alias_method |
|---|---|---|
| Type | Keyword | Method (on Module) |
| Syntax | alias new_name old_name |
alias_method :new_name, :old_name |
| Arguments | Bare names or symbols | Symbols or strings |
| Scope | Lexical (current scope) | Dynamic (can be called programmatically) |
Works in class << self |
Yes | Yes |
| Can be called dynamically | No | Yes |
Practical Use: Wrapping Methods (Before alias_method_chain)
A classic Ruby pattern is to alias the original method, then redefine it to add behavior:
class Array
alias_method :original_push, :push
def push(*args)
puts "Pushing: #{args.inspect}"
original_push(*args)
end
end
arr = []
arr.push(1, 2, 3)
# => Pushing: [1, 2, 3]
puts arr.inspect # => [1, 2, 3]
This is the manual version of what prepend does more cleanly in modern Ruby. Prefer prepend for wrapping methods in new code.
Aliasing for Backward Compatibility
When you rename a method but want to keep the old name working:
class User
def full_name
"#{@first_name} #{@last_name}"
end
# Keep old name working for backward compatibility
alias_method :name, :full_name
end
u = User.new
u.full_name # => "Alice Smith"
u.name # => "Alice Smith" (deprecated alias)
Aliasing with Deprecation Warning
module Deprecatable
def deprecated_alias(new_name, old_name)
alias_method old_name, new_name
define_method(old_name) do |*args, &block|
warn "#{old_name} is deprecated, use #{new_name} instead (called from #{caller.first})"
send(new_name, *args, &block)
end
end
end
class API
extend Deprecatable
def fetch_data(id)
"data for #{id}"
end
deprecated_alias :fetch_data, :get_data
end
api = API.new
api.get_data(42)
# => warning: get_data is deprecated, use fetch_data instead
# => "data for 42"
Dynamic Aliasing
Because alias_method is a regular method, you can call it dynamically:
class Logger
[:info, :warn, :error, :debug].each do |level|
define_method(level) do |msg|
puts "[#{level.upcase}] #{msg}"
end
end
# Alias :log to :info
alias_method :log, :info
end
logger = Logger.new
logger.info("Server started") # => [INFO] Server started
logger.log("Server started") # => [INFO] Server started
logger.error("Disk full") # => [ERROR] Disk full
alias in Singleton Classes
Both alias and alias_method work inside class << self for class-level methods:
class Config
class << self
def load_from_file(path)
puts "Loading from #{path}"
end
alias_method :load, :load_from_file
end
end
Config.load_from_file("config.yml") # => Loading from config.yml
Config.load("config.yml") # => Loading from config.yml
remove_method vs undef_method
When aliasing and cleaning up, you have two options for removing the original:
class Example
def original
"original"
end
alias_method :copy, :original
# remove_method: removes from this class, but superclass version still accessible
remove_method :original
# undef_method: completely blocks the method, even from superclass
# undef_method :original
end
e = Example.new
e.copy # => "original"
e.original # => NoMethodError (removed)
When to Use Each
- Use
aliasfor simple, one-off renaming in class bodies โ it’s more idiomatic Ruby - Use
alias_methodwhen you need dynamic aliasing, or when working inside modules that will be included/extended - Prefer
prependover alias-based wrapping in modern Ruby (2.0+)
Comments