This is an overview of all the special, two-letter (and other) global variables in Ruby, which Ruby inherited from Perl.
For the purpose of improving code readability, Ruby comes with English.rb in its standard library (or Deutsch.rb as gem), which provides non-cryptic aliases and some documentation.
Ruby also defines some three-letter global variables that mirror CLI options ($-…
)
Types of Special Global Variables
Idiosyncratically, not all special global variables are global:
Type | Scope |
---|---|
global | Real global variable |
thread | Thread-global variable: Can have different values in different threads |
pseudo | Looks like a global variable, but is a local variable |
defunct | Not working, anymore (removed from Ruby) |
read-only | Variable cannot be set to a new value |
(read-only) | Variable cannot be set to a new value, but is an array that is mutable |
List of All Special Global Variables
The first table contains all two-letter variables and their aliases:
Perlish | Type | English | Short | Info |
---|---|---|---|---|
$! | thread | $ERROR_INFO | - | RDoc |
$@ | thread | $ERROR_POSITION | - | RDoc |
$; | global | $FIELD_SEPARATOR | $FS, $-F¹ | RDoc |
$, | global | $OUTPUT_FIELD_SEPARATOR | $OFS | RDoc |
$/ | global | $INPUT_RECORD_SEPARATOR | $RS, $-0¹ | RDoc |
$\ | global | $OUTPUT_RECORD_SEPARATOR | $ORS | RDoc |
$. | global | $INPUT_LINE_NUMBER | $NR | RDoc |
$_ | pseudo | $LAST_READ_LINE | - | RDoc |
$> | global | $DEFAULT_OUTPUT | $stdout¹ | IO in Ruby |
$< | global | $DEFAULT_INPUT | - (ARGF) | RDoc |
$$ | read-only | $PROCESS_ID | $PID | Unix Processes |
$? | thread / read-only | $CHILD_STATUS | - | RDoc |
$~ | pseudo | $LAST_MATCH_INFO | - | RDoc |
$= | defunct | $IGNORECASE | - | - |
$* | (read-only) | $ARGV | - | Explanation |
$& | pseudo / read-only | $MATCH | - | RDoc |
$` | pseudo / read-only | $PREMATCH | - | RDoc |
$' | pseudo / read-only | $POSTMATCH | - | RDoc |
$+ | pseudo / read-only | $LAST_PAREN_MATCH² | - | RDoc |
$: | (read-only) | $LOAD_PATH¹ | $-I¹ | RHG: Loading |
$" | (read-only) | $LOADED_FEATURES¹ | - | RHG: Loading |
$0 | global | $PROGRAM_NAME¹ | - | RDoc |
$1 - $9³ | pseudo | - | - | RDoc |
$F⁴ | global | - | - | Auto-Splitting Lines |
¹ Available without requiring English.rb
² No T, because it stands for PARENTHESES, not PARENT
³ Starting with Ruby 2.4, $1 - $9 only get defined when they are currently set
⁴ With command-line option -a
Other Special Global Variables
Name | Type | Info |
---|---|---|
$stdin | global | IO in Ruby |
$stderr | global | IO in Ruby |
$FILENAME | read-only | The current file being read via ARGF. Same as ARGF.filename. |
$DEBUG, $-d | global | Global Debug States |
$VERBOSE, $-v, $-w | global | Global Debug States |
$-W | global | Global Debug States |
$-p | read-only | Stream Editing |
$-l | read-only | Specify Line Format |
$-a | read-only | Auto-Splitting Lines |
$-i | global | Inplace-Editing Files |
More Idiosyncratic Ruby
- Please Comment on GitHub
- Next Article: Know your Environment
- Previous Article: Self Improvement