A feature originally found in Perl, the special variables have special meanings to Ruby. These variables begin with a $ character and consequently are always global, in contrast to Perl which can make them local for certain purposes. (Variables relating to regular expression matching are locally scoped.) Most are named with $ followed by one or more non-alphanumeric characters.

List of special variablesEdit

The special variables in Ruby 1.8 are listed below, and grouped into categories of related variables.

Exception handlingEdit

$!A String giving a message describing the pending exception; this is the second parameter to a raise statement.
$@The backtrace for the pending exception, as an Array of Strings.

Regular expression matchingEdit

These variables are locally scoped in spite of their initial $ character.

$&The complete String that matched the last Regexp, or nil if the match failed.
$`The string preceding the last Regexp match, or nil if the match failed.
$'The string following the last Regexp match, or nil if the match failed.
$+The highest group matched by the last successful Regexp match.
$1, $2, ...The match for the first, second, etc. parenthesized groups in the last regular expression.
$~The MatchData of the last Regexp match in the current scope.

String processingEdit

$=This variable is deprecated and has been removed from Ruby 1.9. To do case-insensitive String compares, use str1.casecmp(str2). When $= is set to a true value, String comparisons map uppercase ASCII letters to lowercase.

Input and output formattingEdit

$/Input record separator. Default setting: "\n". If this is nil, the whole file will be read at once. String methods such as each_line also use this variable.
$\Output record separator. Used by print and IO#write. Default setting: nil.
$,Item to be emitted between the parameters of print and Array#join.
$;Default separator for String#split.
$-0Synonym for $/.
$-FSynonym for $;.
$-lTrue if automatic line ending processing (command line switch -l). $\ is set to the value of $/, and then each line read is processed using chop!.

Standard input and outputEdit

$<An object with the methods of IO, representing the concatenation of all files given by command-line parameters; equivalent to $stdin if no such parameters exist. Similar to Perl's <> file descriptor.
$>The default output for print and printf. Default setting: $stdout.


$$Numeric process ID of the current process.
$*Array containing the command line parameters. Unlike argv in a C program, $* does not include the name of the program; that is found in $0.
$-ISynonym for $:.
$-KThis variable is removed from Ruby 1.9; use str.force_encoding(enc) to set the encoding of an individual string. This may be set to "EUC-JP", "SJIS", "UTF8", "ASCII", or "NONE" to set which of the supported character encodings will be assumed.
$-dTrue if debug mode is enabled (command line switch -d).
$-wSynonym for $-v.
$0The name of the current Ruby script.
$:Array containing the paths from which objects can be loaded.
$?Returns Process::Status for last executed child process (e.g., from `ls -l`)
$"An array containing the filenames of modules loaded by require, read-only.
$_The last line read from STDIN
$DEBUGSynonym for $-d.
$KCODESynonym for $-K.
$LOADED_FEATURESSynonym for $".
$LOAD_PATHSynonym for $:.
$PROGRAM_NAMESynonym for $0.
$SAFELevel for taint and other security-related checks.
$VERBOSESynonym for $-v.
$deferrThis variable is deprecated.
$defoutThis variable is deprecated.

New in Ruby 1.9Edit

The $=, $defout, and $deferr variables, deprecated in Ruby 1.8, disappear altogether in Ruby 1.9. Setting $= will provoke a warning and have no other effect.

$-K and $KCODE are not tagged as deprecated in Ruby 1.8, but nonetheless provoke a warning if used in Ruby 1.9, and have no effect. Strings can now be tagged individually with their encodings.

The $-W variable reflects the warning level of 0, 1, or 2, which may be set on the command line or by assigning $-W.

Community content is available under CC-BY-SA unless otherwise noted.