Skip to content

Ruby Predefined Variables

Ruby provides a set of special predefined global variables. Many of these have short, cryptic names (often involving punctuation like $ followed by a character). While they can be convenient for short scripts or quick debugging in IRB, their use in larger applications or libraries is generally discouraged due to their global nature and potential for reducing code readability.

Many of these variables have more readable alternatives, either through standard methods or by requiring the English module (require 'English'), which provides descriptive aliases.

Below is a table of common predefined variables, their English module aliases (if available), and descriptions:

VariableEnglish AliasDescription
$!$ERROR_INFOThe last exception object raised. Set by rescue. nil if no exception.
$@$ERROR_POSITIONThe stack backtrace for the last exception raised (an array of strings). Use exception.backtrace for a specific exception object.
$_$LAST_READ_LINEThe last string read by gets or readline in the current scope.
$.$INPUT_LINE_NUMBER, $NRThe current input line number of the last file read by gets or from ARGF.
$/$INPUT_RECORD_SEPARATOR, $RSThe input record separator (a string, newline \n by default). gets reads up to this separator.
$\$OUTPUT_RECORD_SEPARATOR, $ORSThe output record separator for print and IO#write ( nil by default). If set, it’s printed after each output.
$,$OUTPUT_FIELD_SEPARATOR, $OFSThe output separator for print’s arguments and Array#join’s default (nil by default).
$;$FIELD_SEPARATOR, $FSThe default separator for String#split when no argument is given (nil by default, which means split by whitespace).
$~$LAST_MATCH_INFOA MatchData object from the last successful regular expression match in the current scope. Use Regexp.last_match for global last match.
$`$PREMATCHThe string to the left of the last successful regex match. From Regexp.last_match.pre_match.
$&$MATCHThe string matched by the last successful regex match. From Regexp.last_match[0].
$‘$POSTMATCHThe string to the right of the last successful regex match. From Regexp.last_match.post_match.
$+$LAST_PAREN_MATCHThe text of the highest-numbered group matched by the last successful regex match.
$1, $2, … $NN/AThe text of the Nth group captured by the last successful regex match. E.g., $1 is Regexp.last_match[1].
$0$PROGRAM_NAMEThe name of the Ruby script file currently being executed.
$*$ARGVAlias for ARGV, the array of command-line arguments passed to the script.
$$$PROCESS_ID, $PIDThe process ID (PID) of the currently running Ruby script.
$?$CHILD_STATUSThe exit status of the last executed child process (a Process::Status object).
$LOAD_PATH, $:$LOAD_PATHAn array of directories Ruby searches when loading files with require or load.
$“$LOADED_FEATURESAn array of filenames of features that have been loaded (required).
$DEBUG$DEBUGA flag indicating if Ruby is running in debug mode (set by the -d command-line option). true or false.
$VERBOSE$VERBOSEThe current verbosity level. nil (silent), false (normal), true (verbose). Set by -w or -v options.
$stdin$stdinThe standard input stream object (default is STDIN).
$stdout$stdoutThe standard output stream object (default is STDOUT).
$stderr$stderrThe standard error stream object (default is STDERR).
$SAFE (Deprecated)N/AThe safe level. Deprecated in Ruby 2.7 and removed in Ruby 3.0. It had values 0-4 to control security restrictions.
$-a, $-d, $-F, $-I, $-l, $-p, $-v, $-w, $-0N/AThese variables reflect the state of various command-line options passed to the Ruby interpreter. For example, $-d is an alias for $DEBUG.
#!/usr/bin/env ruby
require 'English'
# Example of using $~ and $1 after a match
"hello world".match(/(\w+) (\w+)/)
puts "Full match ($& or $MATCH): #{$MATCH}"
puts "First capture ($1): #{$1}"
puts "Second capture ($2): #{$2}"
# Example of using $! and $@ in a rescue block
begin
1 / 0
rescue ZeroDivisionError
puts "Error type ($! or $ERROR_INFO): #{$ERROR_INFO.class}"
puts "Error message: #{$ERROR_INFO.message}"
puts "Backtrace ($@ or $ERROR_POSITION):"
$ERROR_POSITION.first(2).each { |line| puts " #{line}" }
end
puts "Script name ($0 or $PROGRAM_NAME): #{$PROGRAM_NAME}"
puts "Arguments ($* or $ARGV): #{$ARGV.inspect}"
puts "PID ($$ or $PROCESS_ID): #{$PROCESS_ID}"
  • Prefer explicit methods and objects: Instead of relying on $!, use the exception object passed to rescue => e. For MatchData, assign the result of String#match to a variable.
  • Use the English module: If you find yourself needing these global variables, require 'English' can make your code much more readable by providing descriptive aliases (e.g., $ERROR_INFO instead of $!).
  • Limit scope: If used, their impact should be understood and contained, typically within very small scripts or specific local contexts.
  • Avoid modifying most of them: While some (like $/ or $,) can be changed, modifying global state can lead to unexpected behavior in larger programs or libraries.

Many of these variables are remnants from Perl’s influence on Ruby’s early design. Modern Ruby style favors more explicit and object-oriented approaches.

For further details, consult the official Ruby documentation on predefined variables: https://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/globalvars.html