Built-in Variables

Built-in variables are a type of global variable, but their values are not only used globally.

The following are examples of built-in variables categorized by their scope.

Local Scope

$~

The information about the last match in the current scope (MatchData object). Setting this variable changes the values of $&, $1 ... $9 and so on.

Identical to Regexp.last_match.

$&

The string matched by the last successful regular expression pattern match in this scope, or nil if the last pattern match failed.

Identical to Regexp.last_match[0]

$1
$2
$3 ...

Contains the subpattern from the corresponding set of parentheses in the last successful pattern matched, or nil if the last pattern match failed.

Identical to Regexp.last_match[1], Regexp.last_match[2], ...

Global Scope

$!

Information on the latest exception. Set with raise.

$DEBUG

The debug mode flag.