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
vm_method.c
View on GitHub ↗
static VALUE
rb_mod_alias_method(VALUE mod, VALUE newname, VALUE oldname)
{
ID oldid = rb_check_id(&oldname);
if (!oldid) {
rb_print_undef_str(mod, oldname);
}
VALUE id = rb_to_id(newname);
rb_alias(mod, id, oldid);
return ID2SYM(id);
}
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
-
3.0
-
Module#alias_methodnow returns the defined alias as a symbol. [Feature #17314]
-