Ruby Wiki
Advertisement

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 variables[]

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

Exception handling[]

Name Description
$! 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 matching[]

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

Name Description
$& 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 processing[]

Name Description
$= 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 formatting[]

Name Description
$/ 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.
$-0 Synonym for $/.
$-F Synonym for $;.
$-l True 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 output[]

Name Description
$< 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.

Uncategorized[]

Name Description
$$ 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.
$-I Synonym for $:.
$-K This 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.
$-a
$-d True if debug mode is enabled (command line switch -d).
$-i
$-p
$-v
$-w Synonym for $-v.
$. The current (1-indexed) line of input being processed.
$0 The 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
$DEBUG Synonym for $-d.
$FILENAME
$KCODE Synonym for $-K.
$LOADED_FEATURES Synonym for $".
$LOAD_PATH Synonym for $:.
$PROGRAM_NAME Synonym for $0.
$SAFE Level for taint and other security-related checks.
$VERBOSE Synonym for $-v.
$deferr This variable is deprecated.
$defout This variable is deprecated.
$stderr
$stdin
$stdout

New in Ruby 1.9[]

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.

Advertisement