Ruby Predefined Globals

· 4 min read

ruby tips

Below is the summary table for the predefined globals in ruby.

$! $+ $-W $-v $2 $8 $>
$" $, $-a $-w $3 $9 $?
$$ $-0 $-d $. $4 $: $@
$& $-F $-i $/ $5 $; $\
$' $-I $-l $0 $6 $< $_
$* $-K $-p $1 $7 $~ $`
VariableDescription
$! Exception === $! unless $!.nil?

read-only thread-local  The last exception, that was thrown and rescued in the current context.
Locals to the rescue clause.

YOKERHOODCODEPH0ENDPH
$" Array === $"

read-only  Array of strings, containing absolute paths to loaded files.
Files, loaded within both Kernel’s load, require, require_relative directives and platform-specific DL/Fiddle, are shown in the list.

YOKERHOODCODEPH1ENDPH
$$ Fixnum === $$

read-only  Current process ID.

YOKERHOODCODEPH2ENDPH
$& String === $& unless $&.nil?

read-only thread-local  The matched string from the previous successful pattern match.
Locals to the pattern match scope.

YOKERHOODCODEPH3ENDPH
$' String === $' unless $'.nil?

read-only thread-local  The rest of the string after the matched substring in the previous successful pattern match.
Locals to the pattern match scope.

YOKERHOODCODEPH4ENDPH
$* Array === $*

read-only  The command line arguments passed to the currently executed ruby script.
An alias for ARGV.

YOKERHOODCODEPH5ENDPH
$+ String === $+ unless $+.nil?

read-only thread-local  The last captured match from the previous successful pattern match.
Locals to the pattern match scope. Contains nil if there was no capture (even while match was successful.)

YOKERHOODCODEPH6ENDPH
$, String === $, unless $,.nil?

read-write  Separator for both Kernel#print and Array#join.
Defaults to nil.

YOKERHOODCODEPH7ENDPH
$-0

$/ String === $-0 unless $-0.nil?
String === $/ unless $/.nil?

read-write  Input record separator.
Defaults to \n. May be set with the -0 command line parameter (as octal value.)

YOKERHOODCODEPH8ENDPH
$-F

$; String === $-F unless $-F.nil?
String === $: unless $:.nil?

read-write  Default field separator for String#split.
Defaults to nil. May be set with the -F command line parameter.

YOKERHOODCODEPH9ENDPH
$-I

$: Array === $-I
Array === $:

read-only  Array of include paths.
Absolute paths to be searched by Kernel.load and/or Kernel.require.

YOKERHOODCODEPH10ENDPH
$-K String === $-K unless $-K.nil?

obsolete read-write  Determines the encoding to be used whilst parsing .rb files.
One should avoid using that variable since ruby-1.9.

YOKERHOODCODEPH11ENDPH
$-W Fixnum === $-W

read-only  Current verbosity level.
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.

YOKERHOODCODEPH12ENDPH
$-a ∈ [true, false]

read-only  Denotes whether the auto-split mode was enabled with -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.

YOKERHOODCODEPH13ENDPH
$-d ∈ [true, false]

read-only  Denotes whether the debug mode was enabled with -d switch.

YOKERHOODCODEPH14ENDPH
$-i ∈ [true, false]

read-only  Value of the -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.

YOKERHOODCODEPH15ENDPH
$-l ∈ [true, false]

read-only  Denotes whether the automatic line-ending processing mode was enabled with -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!.

YOKERHOODCODEPH16ENDPH
$-p ∈ [true, false]

read-only  Denotes whether the “gets loop around” mode was enabled with -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.

YOKERHOODCODEPH17ENDPH

An example of usage follows:

YOKERHOODCODEPH18ENDPH
$-v

$-w ∈ [true, false]

read-only  Denotes whether the verbose mode was enabled with either -v or -w switch.

YOKERHOODCODEPH19ENDPH
$. Fixnum === $.

read-only  Number of the last line read from the current input file ARGF.
ARGF is a stream designed to use in scripts that process files given as command-line arguments or passed in via STDIN.

YOKERHOODCODEPH20ENDPH
$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?

read-only  The 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.

YOKERHOODCODEPH21ENDPH
$< ARGF === $<

read-only  Read‐only alias for ARGF.

YOKERHOODCODEPH22ENDPH
$= ∈ [true, false]

obsolete  

YOKERHOODCODEPH23ENDPH
$> IO === $>

read-write  Standard output stream.

YOKERHOODCODEPH24ENDPH
$? Process::Status === $? unless $?.nil?

read-only  Exit status of the last terminated process within current context.

YOKERHOODCODEPH25ENDPH
$@ Array === $@ unless $@.nil?

read-only thread-local  Shorthand to $!.backtrace
Locals to the rescue clause.

YOKERHOODCODEPH26ENDPH
$\ String === $\ unless $\.nil?

read-write  Appended to Kernel.print output.
Defaults to nil, or to $/ if the -l switch was given.

YOKERHOODCODEPH27ENDPH
$_ String === $_ unless $_.nil?

read-only thread-local  Last String read from IO by one of Kernel.gets, Kernel.readline or siblings.
Widely used with -p and -n switches.

YOKERHOODCODEPH28ENDPH
$` String === $YOKERHOODCODEPH29ENDPH.nil?

read-only thread-local  The rest of the string before the matched substring in the previous successful pattern match.
Locals to the pattern match scope.

YOKERHOODCODEPH30ENDPH
$~ MatchData === $~ unless $~.nil?

read-only thread-local  The MatchData from the previous successful pattern match.

YOKERHOODCODEPH31ENDPH

Credits to Jim Neath and runpaint , some examples came from these posts.