Ruby Predefined Globals
· 4 min read
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 | $~ | $` |
| Variable | Description |
|---|---|
$!
Exception === $! unless $!.nil?
|
read-only thread-local The last exception, that was thrown and rescued in the current context. |
$"
Array === $"
|
read-only Array of strings,
containing absolute paths to loaded files. |
$$
Fixnum === $$
|
read-only Current process ID. |
$&
String === $& unless $&.nil?
|
read-only thread-local The matched string from the previous successful pattern match. |
$'
String === $' unless $'.nil?
|
read-only thread-local The rest of the string after the matched substring in the previous successful pattern match. |
$*
Array === $*
|
read-only The command line arguments passed to the currently executed ruby script. |
$+
String === $+ unless $+.nil?
|
read-only thread-local The last captured match from the previous successful pattern match. |
$,
String === $, unless $,.nil?
|
read-write Separator for both
|
$-0$/
String === $-0 unless $-0.nil?String === $/ unless $/.nil? |
read-write Input record separator. |
$-F$;
String === $-F unless $-F.nil?String === $: unless $:.nil? |
read-write Default field separator for |
$-I$:
Array === $-IArray === $: |
read-only Array of include paths. |
$-K
String === $-K unless $-K.nil?
|
obsolete read-write Determines the encoding to be used whilst parsing |
$-W
Fixnum === $-W
|
read-only Current verbosity level. |
$-a
∈ [true, false]
|
read-only Denotes whether the auto-split mode was enabled with |
$-d
∈ [true, false]
|
read-only Denotes whether the debug mode was enabled with |
$-i
∈ [true, false]
|
read-only Value of the |
$-l
∈ [true, false]
|
read-only Denotes whether the automatic
line-ending processing mode was enabled with |
$-p
∈ [true, false]
|
read-only Denotes whether the “gets loop around” mode was enabled with An example of usage follows: YOKERHOODCODEPH18ENDPH |
$-v$-w
∈ [true, false]
|
read-only Denotes whether the verbose mode
was enabled with either |
$.
Fixnum === $.
|
read-only Number of the last line read from the current input file |
$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 |
$<
ARGF === $<
|
read-only Read‐only alias for ARGF. |
$=
∈ [true, false]
|
obsolete |
$>
IO === $>
|
read-write Standard output stream. |
$?
Process::Status === $? unless $?.nil?
|
read-only Exit status of the last terminated process within current context. |
$@
Array === $@ unless $@.nil?
|
read-only thread-local Shorthand to |
$\
String === $\ unless $\.nil?
|
read-write Appended to |
$_
String === $_ unless $_.nil?
|
read-only thread-local Last |
$`
String === $YOKERHOODCODEPH29ENDPH.nil?
|
read-only thread-local The
rest of the string before the matched substring in the previous successful pattern match. |
$~
MatchData === $~ unless $~.nil?
|
read-only thread-local The
|
Credits to Jim Neath and runpaint , some examples came from these posts.