December 11, 2006
English Name Equivalents for Ruby's Special Variables
Post by Peter Cooper
The English library (used by simply placing require ‘english’ in your program) allows you to access Ruby’s special variables using names expressed in English, rather than symbols. This makes the variables easier to remember. It is interesting how rarely this library appears to be used in publicly available code, although the use of the special variables does tend to be discouraged. However, the main ones are listed below:
$DEFAULT_OUTPUT (alias for $>) is an alias for the destination of output sent by commands such as print and puts. By default it points to $stdout, the standard output (see the first sidebar in Chapter 9 for more information), typically the screen or current terminal.
$DEFAULT_INPUT (alias for $<) is an object that acts somewhat like a File object for data being sent to the script at the command line, or if these are missing, the standard input (usually the keyboard or current terminal). It is read-only.
$ERROR_INFO (alias for $!) refers to the exception object passed to raise or, more pragmatically, can contain the most recent error message. In the initial form, it can be useful when used within a rescue block.
$ERROR_POSITION (alias for $@) returns a stack trace as generated by the previous exception. This is in the same format as the trace provided by Kernel.caller.
$OFS and $OUTPUT_FIELD_SEPARATOR (aliases for $,) can be set or read and contains the default separator as used in output from the print method and Array’s join method. The default value is nil, as can be confirmed with %w{a b c}.join which results in ‘abc’.
$ORS and $OUTPUT_RECORD_SEPARATOR (aliases for $\) can be set or read and contains the default separator as used when sending output with methods such as print and IO.write. Its default value is nil, as typically puts is used instead when you want to append a newline to data being sent.
$FS and $FIELD_SEPARATOR (aliases for $;) can be set or read and contains the default separator as used by String’s split method. Changing this and then calling split on a string without a split regex or character can give different results than expected.
$RS and $INPUT_RECORD_SEPARATOR (aliases for $/) can be set or read and contains the default separator as used for input such as from gets. The default value is a newline (\n) and results in gets receiving one line at a time. If this value is set to nil, then an entire file or data stream would be read by gets in one go.
$PID and $PROCESS_ID (alias for $$) returns the process ID of the current program. This ID is unique for every program or instance of a program running on a computer, which is why it is used by Tempfile when constructing temporary file names. It is read-only.
$LAST_MATCH_INFO (alias for $~) returns a MatchData object that contains the results of the last successful pattern match.
$IGNORECASE (alias for $=) is a flag that you can set or read from that determines whether regular expressions and pattern matches performed in the program will be case insensitive by default. This special variable is deprecated and may be removed in Ruby 2. Typically if you required this feature you would use the /i flag on the end of a regular expression instead.
$MATCH (alias for $&) contains the entire string matched by the last successful regular expression match in the current scope. If there has been no match, its value is nil.
$PREMATCH (alias for $`) contains the string preceding the match discovered by the last successful regular expression match in the current scope. If there has been no match, its value is nil.
$POSTMATCH (alias for $’) contains the string succeeding the match discovered by the last successful regular expression match in the current scope. If there has been no match, its value is nil.