Ruby Programming/Reference/Predefined Variables

Ruby's predefined (built-in) variables affect the behavior of the entire program, so their use in libraries isn't recommended. The values in most predefined variables can be accessed by alternative means.

$!
 * The last exception object raised. The exception object can also be accessed using => in rescue clause.

$@
 * The stack backtrace for the last exception raised. The stack backtrace information can retrieved by Exception#backtrace method of the last exception.

$/
 * The input record separator (newline by default). gets, readline, etc., take their input record separator as optional argument.

$\
 * The output record separator (nil by default).

$,
 * The output separator between the arguments to print and Array#join (nil by default). You can specify separator explicitly to Array#join.

$;
 * The default separator for split (nil by default). You can specify separator explicitly for String#split.

$.
 * The number of the last line read from the current input file. Equivalent to ARGF.lineno.

$<
 * Synonym for ARGF.

<tt>$></tt>
 * Synonym for $defout.

<tt>$0</tt>
 * The name of the current Ruby program being executed.

<tt>$$</tt>
 * The process.pid of the current Ruby program being executed.

<tt>$?</tt>
 * The exit status of the last process terminated.

<tt>$:</tt>
 * Synonym for $LOAD_PATH.

<tt>$DEBUG</tt>
 * True if the -d or --debug command-line option is specified.

<tt>$defout</tt>
 * The destination output for print and printf ($stdout by default).

<tt>$F</tt>
 * The variable that receives the output from split when -a is specified. This variable is set if the -a command-line option is specified along with the -p or -n option.

<tt>$FILENAME</tt>
 * The name of the file currently being read from ARGF. Equivalent to ARGF.filename.

<tt>$LOAD_PATH</tt>
 * An array holding the directories to be searched when loading files with the load and require methods.

<tt>$SAFE</tt> 0  No checks are performed on externally supplied (tainted) data. (default)
 * The security level.

1  Potentially dangerous operations using tainted data are forbidden.

2  Potentially dangerous operations on processes and files are forbidden.

3  All newly created objects are considered tainted.

4  Modification of global data is forbidden.

<tt>$stdin</tt>
 * Standard input (STDIN by default).

<tt>$stdout</tt>
 * Standard output (STDOUT by default).

<tt>$stderr</tt>
 * Standard error (STDERR by default).

<tt>$VERBOSE</tt>
 * True if the -v, -w, or --verbose command-line option is specified.

<tt>$- x</tt>
 * The value of interpreter option -x (x=0, a, d, F, i, K, l, p, v).

The following are local variables:

<tt>$_</tt>
 * The last string read by gets or readline in the current scope.

<tt>$~</tt>
 * MatchData relating to the last match. Regex#match method returns the last match information.

The following variables hold values that change in accordance with the current value of $~ and can't receive assignment:

<tt>$ n ($1, $2, $3...)</tt>
 * The string matched in the nth group of the last pattern match. Equivalent to m[n], where m is a MatchData object.

<tt>$&</tt>
 * The string matched in the last pattern match. Equivalent to m[0], where m is a MatchData object.

<tt>$`</tt>
 * The string preceding the match in the last pattern match. Equivalent to m.pre_match, where m is a MatchData object.

<tt>$'</tt>
 * The string following the match in the last pattern match. Equivalent to m.post_match, where m is a MatchData object.

<tt>$+</tt>
 * The string corresponding to the last successfully matched group in the last pattern match.