module
Crystallabs::Helpers::Alias_Methods
Overview
ameba:disable Naming/TypeNames -- deliberate public API name (released, snake_case mirrors alias_method)
Defined in:
crystallabs-helpers.crMacro Summary
-
alias_method(new_method, old_method)
Defines new_method as an alias of old_method.
-
alias_previous(*new_methods)
Defines new_method as an alias of the immediately preceding method.
Macro Detail
Defines new_method as an alias of old_method.
One forwarder is defined per overload of old_method, reproducing that overload's parameter list verbatim — restrictions, defaults, keyword-only section and block argument included. So the alias is exactly as type-safe as what it aliases, and named arguments and blocks both work.
class Person
getter name
def initialize(@name)
end
alias_method full_name, name
end
person = Person.new "John"
person.name # => "John"
person.full_name # => "John"
Copying the restrictions is what makes an alias safe to introduce next to
an existing method of the same name inherited from elsewhere. An
unrestricted def alias(*args) forwarder would sit closer in the ancestor
chain than that inherited method and silently swallow every call to it; a
restricted one only claims the argument types it actually handles, leaving
the rest to resolve as before.
This macro was present in Crystal until commit 7c3239ee505e07544ec372839efed527801d210a.
Defines new_method as an alias of the immediately preceding method.
NOTE only use this when the preceding method has a single overload.
@type.methods does not preserve source order once a name is overloaded,
so "the last one" is not reliably the one written just above. Name the
target explicitly with alias_method in that case.