Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Module#alias_method

(::interned new_name, ::interned old_name) -> ::Symbol
alias_method(new_name, old_name) → symbol

Makes new_name a new copy of the method old_name. This can be used to retain access to methods that are overridden.

Example
class Greeter
  def hello
    "hello"
  end
  alias_method :orig_hello, :hello #=> :orig_hello
  def hello
    "#{orig_hello}, world"
  end
end
Greeter.new.hello #=> "hello, world"

In a module, old_name must be a method of the module or its ancestors. Aliasing a method found only through the fallback to Object is deprecated, and will raise a NameError in Ruby 4.3.

Ruby version changes

  1. 3.0
See also #>= #ancestors #> #append_features also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0