Ruby Keyword: alias - rking/pry-docmore GitHub Wiki

The alias keyword is used to make synonyms of methods.

If you are used to the semantics of the /bin/ln command, get it out of your head right now: Ruby does the opposite. That is, instead of ln existing new, Ruby's semantics are alias new existing. (Mnested Mnemonic: this new sorting is the ba-n-e of my existence.)

Also take particular note that this is not a method call, but a keyword, so it can do things method calls cannot. The args are not :symbols (though they are permitted to be), and it is a SyntaxError to attempt to place a comma between the args.

Note that, this being a keyword and happening at compile-time, is not enough to make dynamic assignments. See {Module#alias_method} for that kind of trickery.

Basic Usage

def foo; 3 end alias boo foo boo ⇒ 3

Symbols as args

alias :zoo :boo # or even mix and match, if you want to for some reason. zoo ⇒ 3

Some decorating!

alias orig_foo foo def foo; 4 + orig_foo end foo ⇒ 7

See also Rails's alias_method_chain for a formalization of this idiom.

See also: http://kresimirbojcic.com/2011/12/01/decorators-in-ruby.html

⚠️ **GitHub.com Fallback** ⚠️