Advent of Prism: Part 20 - Alias and undef

This blog series is about how the prism Ruby parser works. If you’re new to the series, I recommend starting from the beginning. This post is about the alias and undef keywords.

These two keywords are not often used, largely because there are methods that can be called to do the same thing. However, they are still a part of the Ruby language.


The alias keyword allows you to create an alias for a method. For example:

alias new_name old_name

This creates a new method called new_name that is an alias for the old_name method from the current context. This is represented by the following AST:

alias node

We represent the names of the methods with symbols even if they are bare words because they can also be symbols. A semantically equivalent example to the above using symbols would be:

alias :new_name :old_name

Any method name at all can be used, including those that are not valid Ruby identifiers. For example, the following is valid:

alias push <<

You can also use dynamic method names with interpolated symbols, as in:

new_prefix = "new"
old_prefix = "old"
alias :"#{new_prefix}_name" :"#{old_prefix}_name"

This is semantically equivalent to the first example. This is represented by:

alias method node


You can also alias global variables. For example:

alias $new_name $old_name

This is represented by:

alias global variable node

This is particularly useful for providing longer names for global variables that are used often. As an example, see the English.rb core Ruby library.


The undef keyword allows you to undefine a method. For example:

undef foo

This is represented by:

undef node

Much like the alias keyword, we use symbols to represent the method names even if they are bare words. undef accepts multiple method names, so the following is also valid:

undef :foo, :bar, :baz

This is represented by:

undef node

Finally, you can also use dynamic symbols, as in:

undef :"foo_#{bar}"

Wrapping up

The alias and undef keywords are not found very often but they are pieces of syntax that stretch back as far as Ruby 1.0. Here are a couple of things to remember from today:

In the next post, we’ll be looking at throws and jumps.

← Back to home