Back to Ruby

Pre-Defined Global Variables

doc/language/globals.md

4.0.317.3 KB
Original Source

Pre-Defined Global Variables

Some of the pre-defined global variables have synonyms that are available via module English. For each of those, the \English synonym is given.

To use the module:

ruby
require 'English'

In Brief

Exceptions

Variable\EnglishContainsInitiallyRead-OnlyReset By
$!$ERROR_INFO\Exception object or nilnilYesKernel#raise
$@$ERROR_POSITION\Array of backtrace positions or nilnilYesKernel#raise

Matched \Data

Variable\EnglishContainsInitiallyRead-OnlyReset By
$~$LAST_MATCH_INFO\MatchData object or nilnilNoMatcher methods
$&$MATCHMatched substring or nilnilNoMatcher methods
$`$PRE_MATCHSubstring left of match or nilnilNoMatcher methods
$'$POST_MATCHSubstring right of match or nilnilNoMatcher methods
$+$LAST_PAREN_MATCHLast group matched or nilnilNoMatcher methods
$1First group matched or nilnilYesMatcher methods
$2Second group matched or nilnilYesMatcher methods
<tt>$n</tt><i>n</i>th group matched or nilnilYesMatcher methods

Separators

Variable\EnglishContainsInitiallyRead-OnlyReset By
$/, $-0$INPUT_RECORD_SEPARATORInput record separatorNewlineNo
$\$OUTPUT_RECORD_SEPARATOROutput record separatornilNo

Streams

Variable\EnglishContainsInitiallyRead-OnlyReset By
$stdinStandard input streamSTDINNo
$stdoutStandard output streamSTDOUTNo
$stderrStandard error streamSTDERRNo
$<$DEFAULT_INPUTDefault standard inputARGFYes
$>$DEFAULT_OUTPUTDefault standard outputSTDOUTNo
$.$INPUT_LINE_NUMBER, $NRInput position of most recently read stream0NoCertain read methods
$_$LAST_READ_LINEString from most recently read streamnilNoCertain read methods

Processes

Variable\EnglishContainsInitiallyRead-OnlyReset By
$0, $PROGRAM_NAMEProgram nameProgram nameNo
$*$ARGV\ARGV arrayARGVYes
$$$PROCESS_ID, $PIDProcess idProcess PIDYes
$?$CHILD_STATUSStatus of recently exited childnilYes
$LOAD_PATH, $:, $-I\Array of search pathsRuby defaultsYes
$LOADED_FEATURES, $"\Array of load pathsRuby defaultsYes

Debugging

Variable\EnglishContainsInitiallyRead-OnlyReset By
$FILENAMEValue returned by method ARGF.filenameCommand-line argument or '-'Yes
$DEBUGWhether option -d or --debug was givenCommand-line optionNo
$VERBOSEWhether option -V or -W was givenCommand-line optionNo

Other Variables

Variable\EnglishContainsInitiallyRead-OnlyReset By
$-F, $;Separator given with command-line option -F
$-aWhether option -a was givenYes
$-iExtension given with command-line option -iNo
$-lWhether option -l was givenYes
$-pWhether option -p was givenYes
$F\Array of $_ split by $-F

Exceptions

$! (\Exception)

Contains the Exception object set by Kernel#raise:

ruby
begin
  raise RuntimeError.new('Boo!')
rescue RuntimeError
  p $!
end

Output:

#<RuntimeError: Boo!>

English - $ERROR_INFO

$@ (Backtrace)

Same as $!.backtrace; returns an array of backtrace positions:

ruby
begin
  raise RuntimeError.new('Boo!')
rescue RuntimeError
  pp $@.take(4)
end

Output:

["(irb):338:in `<top (required)>'",
 "/snap/ruby/317/lib/ruby/3.2.0/irb/workspace.rb:119:in `eval'",
 "/snap/ruby/317/lib/ruby/3.2.0/irb/workspace.rb:119:in `evaluate'",
 "/snap/ruby/317/lib/ruby/3.2.0/irb/context.rb:502:in `evaluate'"]

English - $ERROR_POSITION.

Matched \Data

These global variables store information about the most recent successful match in the current scope.

For details and examples, see {Regexp Global Variables}[rdoc-ref:Regexp@Global+Variables].

$~ (\MatchData)

MatchData object created from the match; thread-local and frame-local.

English - $LAST_MATCH_INFO.

$& (Matched Substring)

The matched string.

English - $MATCH.

$` (Pre-Match Substring)

The string to the left of the match.

English - $PREMATCH.

$' (Post-Match Substring)

The string to the right of the match.

English - $POSTMATCH.

$+ (Last Matched Group)

The last group matched.

English - $LAST_PAREN_MATCH.

$1, $2, \Etc. (Matched Group)

For <tt>$n</tt> the <i>n</i>th group of the match.

No \English.

Separators

$/ (Input Record Separator)

An input record separator, initially newline. Set by the command-line option -0.

Setting to non-nil value by other than the command-line option is deprecated.

English - $INPUT_RECORD_SEPARATOR, $RS.

Aliased as $-0.

$\ (Output Record Separator)

An output record separator, initially nil.

Copied from $/ when the command-line option -l is given.

Setting to non-nil value by other than the command-line option is deprecated.

English - $OUTPUT_RECORD_SEPARATOR, $ORS.

Streams

$stdin (Standard Input)

The current standard input stream; initially:

ruby
$stdin # => #<IO:<STDIN>>

$stdout (Standard Output)

The current standard output stream; initially:

ruby
$stdout # => #<IO:<STDOUT>>

$stderr (Standard Error)

The current standard error stream; initially:

ruby
$stderr # => #<IO:<STDERR>>

$< (\ARGF or $stdin)

Points to stream ARGF if not empty, else to stream $stdin; read-only.

English - $DEFAULT_INPUT.

$> (Default Standard Output)

An output stream, initially $stdout.

English - $DEFAULT_OUTPUT

$. (Input Position)

The input position (line number) in the most recently read stream.

English - $INPUT_LINE_NUMBER, $NR

$_ (Last Read Line)

The line (string) from the most recently read stream.

English - $LAST_READ_LINE.

Processes

$0

Initially, contains the name of the script being executed; may be reassigned.

$* (\ARGV)

Points to ARGV.

English - $ARGV.

$$ (Process ID)

The process ID of the current process. Same as Process.pid.

English - $PROCESS_ID, $PID.

$? (Child Status)

Initially nil, otherwise the Process::Status object created for the most-recently exited child process; thread-local.

English - $CHILD_STATUS.

$LOAD_PATH (Load Path)

Contains the array of paths to be searched by Kernel#load and Kernel#require.

Singleton method $LOAD_PATH.resolve_feature_path(feature) returns:

  • <tt>[:rb, path]</tt>, where path is the path to the Ruby file to be loaded for the given feature.
  • <tt>[:so, path]</tt>, where path is the path to the shared object file to be loaded for the given feature.
  • nil if there is no such feature and path.

Examples:

ruby
$LOAD_PATH.resolve_feature_path('timeout')
# => [:rb, "/snap/ruby/317/lib/ruby/3.2.0/timeout.rb"]
$LOAD_PATH.resolve_feature_path('date_core')
# => [:so, "/snap/ruby/317/lib/ruby/3.2.0/x86_64-linux/date_core.so"]
$LOAD_PATH.resolve_feature_path('foo')
# => nil

Aliased as $: and $-I.

$LOADED_FEATURES

Contains an array of the paths to the loaded files:

ruby
$LOADED_FEATURES.take(10)
# =>
["enumerator.so",
 "thread.rb",
 "fiber.so",
 "rational.so",
 "complex.so",
 "ruby2_keywords.rb",
 "/snap/ruby/317/lib/ruby/3.2.0/x86_64-linux/enc/encdb.so",
 "/snap/ruby/317/lib/ruby/3.2.0/x86_64-linux/enc/trans/transdb.so",
 "/snap/ruby/317/lib/ruby/3.2.0/x86_64-linux/rbconfig.rb",
 "/snap/ruby/317/lib/ruby/3.2.0/rubygems/compatibility.rb"]

Aliased as $".

Debugging

$FILENAME

The value returned by method ARGF.filename.

$DEBUG

Initially true if command-line option -d or --debug is given, otherwise initially false; may be set to either value in the running program.

When true, prints each raised exception to $stderr.

Aliased as $-d.

$VERBOSE

Initially true if command-line option -v or -w is given, otherwise initially false; may be set to either value, or to nil, in the running program.

When true, enables Ruby warnings.

When nil, disables warnings, including those from Kernel#warn.

Aliased as $-v and $-w.

Other Variables

$-F

The default field separator in String#split; must be a String or a Regexp, and can be set with command-line option -F.

Setting to non-nil value by other than the command-line option is deprecated.

Aliased as $;.

$-a

Whether command-line option -a was given; read-only.

$-i

Contains the extension given with command-line option -i, or nil if none.

An alias of ARGF.inplace_mode.

$-l

Whether command-line option -l was set; read-only.

$-p

Whether command-line option -p was given; read-only.

$F

If the command-line option -a is given, the array obtained by splitting $_ by $-F is assigned at the start of each -l/-p loop.

Deprecated

$=

$,

Pre-Defined Global Constants

Summary

Streams

ConstantContains
STDINStandard input stream.
STDOUTStandard output stream.
STDERRStandard error stream.

Environment

ConstantContains
ENVHash of current environment variable names and values.
ARGFString concatenation of files given on the command line, or $stdin if none.
ARGVArray of the given command-line arguments.
TOPLEVEL_BINDINGBinding of the top level scope.
RUBY_VERSIONString Ruby version.
RUBY_RELEASE_DATEString Ruby release date.
RUBY_PLATFORMString Ruby platform.
RUBY_PATCH_LEVELString Ruby patch level.
RUBY_REVISIONString Ruby revision.
RUBY_COPYRIGHTString Ruby copyright.
RUBY_ENGINEString Ruby engine.
RUBY_ENGINE_VERSIONString Ruby engine version.
RUBY_DESCRIPTIONString Ruby description.

Embedded \Data

ConstantContains
DATAFile containing embedded data (lines following __END__, if any).

Streams

STDIN

The standard input stream (the default value for $stdin):

ruby
STDIN # => #<IO:<STDIN>>

STDOUT

The standard output stream (the default value for $stdout):

ruby
STDOUT # => #<IO:<STDOUT>>

STDERR

The standard error stream (the default value for $stderr):

ruby
STDERR # => #<IO:<STDERR>>

Environment

ENV

A hash of the contains current environment variables names and values:

ruby
ENV.take(5)
# =>
[["COLORTERM", "truecolor"],
 ["DBUS_SESSION_BUS_ADDRESS", "unix:path=/run/user/1000/bus"],
 ["DESKTOP_SESSION", "ubuntu"],
 ["DISPLAY", ":0"],
 ["GDMSESSION", "ubuntu"]]

ARGF

The virtual concatenation of the files given on the command line, or from $stdin if no files were given, "-" is given, or after all files have been read.

ARGV

An array of the given command-line arguments.

TOPLEVEL_BINDING

The Binding of the top level scope:

ruby
TOPLEVEL_BINDING # => #<Binding:0x00007f58da0da7c0>

RUBY_VERSION

The Ruby version:

ruby
RUBY_VERSION # => "3.2.2"

RUBY_RELEASE_DATE

The release date string:

ruby
RUBY_RELEASE_DATE # => "2023-03-30"

RUBY_PLATFORM

The platform identifier:

ruby
RUBY_PLATFORM # => "x86_64-linux"

RUBY_PATCHLEVEL

The integer patch level for this Ruby:

ruby
RUBY_PATCHLEVEL # => 53

For a development build the patch level will be -1.

RUBY_REVISION

The git commit hash for this Ruby:

ruby
RUBY_REVISION # => "e51014f9c05aa65cbf203442d37fef7c12390015"

The copyright string:

ruby
RUBY_COPYRIGHT
# => "ruby - Copyright (C) 1993-2023 Yukihiro Matsumoto"

RUBY_ENGINE

The name of the Ruby implementation:

ruby
RUBY_ENGINE # => "ruby"

RUBY_ENGINE_VERSION

The version of the Ruby implementation:

ruby
RUBY_ENGINE_VERSION # => "3.2.2"

RUBY_DESCRIPTION

The description of the Ruby implementation:

ruby
RUBY_DESCRIPTION
# => "ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-linux]"

Embedded \Data

DATA

Defined if and only if the program has this line:

ruby
__END__

When defined, DATA is a File object containing the lines following the __END__, positioned at the first of those lines:

ruby
p DATA
DATA.each_line { |line| p line }
__END__
Foo
Bar
Baz

Output:

#<File:t.rb>
"Foo\n"
"Bar\n"
"Baz\n"