Ruby Predefined Globals
· 10 min read
Below is the summary table for the predefined globals in ruby.
<table class="table table-bordered code">
<tbody>
<tr>
<td><a href="#bang" class="btn"><strong>$!</strong></a></td>
<td><a href="#plus" class="btn"><strong>$+</strong></a></td>
<td><a href="#minus-w" class="btn"><strong>$-W</strong></a></td>
<td><a href="#minus-v-small" class="btn"><strong>$-v</strong></a></td>
<td><a href="#digit" class="btn"><strong>$2</strong></a></td>
<td><a href="#digit" class="btn"><strong>$8</strong></a></td>
<td><a href="#greater" class="btn"><strong>$></strong></a></td>
</tr>
<tr>
<td><a href="#double-quotes" class="btn"><strong>$"</strong></a></td>
<td><a href="#comma" class="btn"><strong>$,</strong></a></td>
<td><a href="#minus-a-small" class="btn"><strong>$-a</strong></a></td>
<td><a href="#minus-w-small" class="btn"><strong>$-w</strong></a></td>
<td><a href="#digit" class="btn"><strong>$3</strong></a></td>
<td><a href="#digit" class="btn"><strong>$9</strong></a></td>
<td><a href="#qmark" class="btn"><strong>$?</strong></a></td>
</tr>
<tr>
<td><a href="#dollar" class="btn"><strong>$$</strong></a></td>
<td><a href="#minus-zero" class="btn"><strong>$-0</strong></a></td>
<td><a href="#minus-d-small" class="btn"><strong>$-d</strong></a></td>
<td><a href="#dot" class="btn"><strong>$.</strong></a></td>
<td><a href="#digit" class="btn"><strong>$4</strong></a></td>
<td><a href="#colon" class="btn"><strong>$:</strong></a></td>
<td><a href="#at" class="btn"><strong>$
@
</tr>
<tr>
<td><a href="#ampersand" class="btn"><strong>$&</strong></a></td>
<td><a href="#minus-f" class="btn"><strong>$-F</strong></a></td>
<td><a href="#minus-i-small" class="btn"><strong>$-i</strong></a></td>
<td><a href="#slash" class="btn"><strong>$/</strong></a></td>
<td><a href="#digit" class="btn"><strong>$5</strong></a></td>
<td><a href="#semicolon" class="btn"><strong>$;</strong></a></td>
<td><a href="#backslash" class="btn"><strong>$</a></td>
</tr>
<tr>
<td><a href="#end-quote" class="btn"><strong>$'</strong></a></td>
<td><a href="#minus-i" class="btn"><strong>$-I</strong></a></td>
<td><a href="#minus-l-small" class="btn"><strong>$-l</strong></a></td>
<td><a href="#digit" class="btn"><strong>$0</strong></a></td>
<td><a href="#digit" class="btn"><strong>$6</strong></a></td>
<td><a href="#less" class="btn"><strong>$<</strong></a></td>
<td><a href="#underscore" class="btn"><strong>$
</strong></a></td>
</tr>
<tr>
<td><a href="#splat" class="btn"><strong>$
</strong></a></td>
<td><a href="#minus-k" class="btn"><strong>$-K</strong></a></td>
<td><a href="#minus-p-small" class="btn"><strong>$-p</strong></a></td>
<td><a href="#digit" class="btn"><strong>$1</strong></a></td>
<td><a href="#digit" class="btn"><strong>$7</strong></a></td>
<td><a href="#tilde" class="btn"><strong>$
read-only thread-local The last exception, that was thrown and rescued in the current context. read-only Array of strings,
containing absolute paths to loaded files. read-only Current process ID. read-only thread-local The matched string from the previous successful pattern match. read-only thread-local The rest of the string after the matched substring in the previous successful pattern match. read-only The command line arguments passed to the currently executed ruby script. read-only thread-local The last captured match from the previous successful pattern match. read-write Separator for both
read-write Input record separator. read-write Default field separator for read-only Array of include paths. obsolete read-write Determines the encoding to be used whilst parsing read-only Current verbosity level. read-only Denotes whether the auto-split mode was enabled with read-only Denotes whether the debug mode was enabled with read-only Value of the read-only Denotes whether the automatic
line-ending processing mode was enabled with read-only Denotes whether the “gets loop around” mode was enabled with An example of usage follows: read-only Denotes whether the verbose mode
was enabled with either read-only Number of the last line read from the current input file read-only The read-only Read‐only alias for ARGF. obsolete read-write Standard output stream. read-only Exit status of the last terminated process within current context. read-only thread-local Shorthand to read-write Appended to read-only thread-local Last
</td>
</tr>
<tr>
<td class="tooltipable">
<a id="tilde"></a>
<code class="btn btn-large disabled"><strong>$
</td>
</tr>
</tbody>
</table>
</div>
Credits to
Jim Neath
and
runpaint
, some examples came from these posts.
We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
</strong></a></td>
<td><a href="#backtick" class="btn"><strong>$
Variable Description
$!
Exception === $! unless $!.nil?
Locals to the rescue clause.
$"
Array === $"
Files, loaded within both Kernel’s load,
require, require_relative directives and platform-specific
DL/Fiddle,
are shown in the list.
$$
Fixnum === $$
$&
String === $& unless $&.nil?
Locals to the pattern match scope.
$'
String === $' unless $'.nil?
Locals to the pattern match scope.
$*
Array === $*
An alias for ARGV.
$+
String === $+ unless $+.nil?
Locals to the pattern match scope. Contains nil if there was no capture (even while match was successful.)
$,
String === $, unless $,.nil?
Kernel#print and Array#join.
Defaults to nil.
$-0
$/
String === $-0 unless $-0.nil?
String === $/ unless $/.nil?
Defaults to \n. May be set with the -0 command line parameter (as octal value.)
$-F
$;
String === $-F unless $-F.nil?
String === $: unless $:.nil?
String#split.
Defaults to nil. May be set with the -F command line parameter.
$-I
$:
Array === $-I
Array === $:
Absolute paths to be searched by Kernel.load and/or Kernel.require.
$-K
String === $-K unless $-K.nil?
.rb files.
One should avoid using that variable since ruby-1.9.
$-W
Fixnum === $-W
Defaults to 1. May be set to 0 with the -W0 command line arg
and to 2 with one of -w, -v, or --verbose switches.
$-a
∈ [true, false]
-a command line argument.
-a switch makes sense when used together with either -p
or -n args.
In auto-split mode, Ruby executes $F = $_.split at the beginning of each loop.
$-d
∈ [true, false]
-d switch.
$-i
∈ [true, false]
-i command line argument, if given.
Defaults to nil. When provided, this argument enables inplace-edit mode; the parameter specifies an extension of
backup file to be created.
$-l
∈ [true, false]
-l switch.
Automatic line-endings sets $\ to
the value of $/, and (when used with either -p
or -n args) chops every line read using chop!.
$-p
∈ [true, false]
-p switch.
-p switch acts mostly like -n sibling with one exception: it prints the value
of variable $_ at the each end of the loop.
For some unknown reason there is no internal global
to get aknowledged whether the -n command line switch was given. In case it were, Ruby is to assume
the following loop around your script, which provides an iteration over filename arguments somewhat like
sed -n and awk do.
ruby
~ echo "foo bar baz" | ruby -p -e '$_.tr! "o-z", "O-Z"'
# ⇒ fOO baR baZ
$-v
$-w
∈ [true, false]
-v or -w switch.
$.
Fixnum === $.
ARGF.
ARGF is a stream designed to use in scripts that process files given as command-line arguments or passed in via STDIN.
$0…$9
String === $0 unless $0.nil?
String === $1 unless $1.nil?
String === $2 unless $2.nil?
String === $3 unless $3.nil?
String === $4 unless $4.nil?
String === $5 unless $5.nil?
String === $6 unless $6.nil?
String === $7 unless $7.nil?
String === $8 unless $8.nil?
String === $9 unless $9.nil?
N-th capture of the previous successful pattern match.
Defaults to nil if the match failed or if N is greater than an amount of captured groups.
$<
ARGF === $<
$=
∈ [true, false]
$>
IO === $>
$?
Process::Status === $? unless $?.nil?
$@
Array === $@ unless $@.nil?
$!.backtrace
Locals to the rescue clause.
$\
String === $\ unless $\.nil?
Kernel.print output.
Defaults to nil, or to $/ if the -l switch was given.
$_
String === $_ unless $_.nil?
String read from IO
by one of Kernel.gets, Kernel.readline or siblings.
Widely used with -p and -n switches.
$
</strong></code>
<span class="code">String === $
unless $
.nil?</span>
</td>
<td>
<p><span class="label label-important">read-only</span> <span class="label label-info">thread-local</span> <strong>The
rest of the string <em>before</em> the matched substring in the previous successful pattern match.</strong><br>
Locals to the pattern match scope.</p>
> "foo bar baz".match /bar|baz/
# ⇒ <MatchData "bar">
> $`
# ⇒ "foo "
> "foo bar baz".gsub /foo|bar|baz/, 'ggg'
# ⇒ "ggg ggg ggg"
> $`
# ⇒ "foo bar "
> "foo bar baz".match /foobarbaz/
# ⇒ nil
> $`
# ⇒ nil
</strong></code>
<span class="code">MatchData === $
unless $
.nil?</span>
</td>
<td>
<p><span class="label label-important">read-only</span> <span class="label label-info">thread-local</span> <strong>The
<code>MatchData</code> from the previous successful pattern match.</strong><br></p>
> "abc12def34ghijklmno567pqrs".gsub (/\d+/) { |m| p $~ }
# ⇒ <MatchData "12">
# ⇒ <MatchData "34">
# ⇒ <MatchData "567">