Top Level Namespace

Defined in:

Macro Summary

Macro Detail

macro alias_method(new, old, yield_arity = 0, redefine = false) #

The alias_method macro is used to create method aliases.

Macro arguments:

  • new: the alias; the new name for the method. This is required.
  • old: the original method name; this is the method that the alias will point to.
  • yield_arity: the expected arity of the block that the method being aliased will yield to. This argument is optional, and is only required is the aliased method yields, and the block that it yields to is expected to have an arity other than 0.
  • redefine: normally, when a method is aliased for the first time, a new, canonical copy of it is created, and both the original name and the alias point to the same version of the method. If additional aliases to that same method are created, that canonical version of the method will not be redefined; all aliases will point to the same implementation. When creating alias method chains, however, the aforementioned behavior prevents the formation of a chain of method calls. Each newly created alias will point to the same method.

For example, alias_method get, :[] creates an alias from the []() method to the get() method.

The methods to be aliased can be specified as symbol literals, string literals, or via direct method references. The macro will not throw any errors if the method being aliased can not be found.For example, consider the following examples:

class MyClass
  def self.add(x, y)
    x + y

  def with(arg)
    yield arg

  def [](val)
    val ** 3

  def chain(ary)
    ary << "a"

  alias_method "self.suma", "self.add" # Class method alias
  alias_method suma, MyClass.add       # Instance method alias to a class method
  alias_method con, :with, 1           # Alias to a method that yields
  alias_method cube, :[]               # Alias to a method name that has punctuation
  alias_method nada, nothing           # Alias to a method that doesn't exist (no error)

  alias_method chain_a, chain          # Create a chain of aliases
  def chain(ary)
    chain_a(ary) << "b"
  alias_method chain_b, chain
  def chain(ary)
     chain_b(ary) << "c"

foo =

puts "Call the MyClass.add class method via the class method alias, MyClass.suma: #{MyClass.suma(123, 456)}"
puts "Call the MyClass.add class method via the instance method alias, MyClass#suma: #{foo.suma(456, 789)}"
puts "Call an alias to a method that takes a block: #{foo.con(7) {|x| x ** x}}"
puts "Call a method that forms a chain of aliased methods: #{foo.chain([] of String).inspect}"

[View source]
macro remove_method(old) #

This macro removes a method. It is not possible to actually undefined a method in Crystal, so this macro redefines the method to return, at runtime, a NoMethodError exception.

Method removal works on both class methods and instance methods. If you have a method chain that has been created through multiple layers of methods, and one of the links in the middle of the chain is removed, it will break the chain, so be careful with that.

Methods to be removed can be specified directly, through StringLiterals, or through Symbol literals, just like alias_method.

class Foo
  def original_method
    "do method stuff"

  alias_method dup_original_method, original_method
  alias_method copy_original_method, original_method
  alias_method extra_original_method, original_method

  # Use a naked method name:
  remove_method original_method

  # Use a string:
  remove_method "extra_original_method"

  # Use a symbol:
  remove_method :copy_original_method

This can be convenient when aliasing class methods.

module Benchmark
  alias_method "self.instructions_per_second", "self.ips"

[View source]