Ruby Predefined Constants
Ruby Predefined Constants
Section titled “Ruby Predefined Constants”Ruby provides a set of predefined constants that are available throughout your programs. Understanding these can be helpful, though some are used more frequently than others.
The following table lists some of Ruby’s most notable predefined constants:
NOTE: While TRUE, FALSE, and NIL (all uppercase) exist for backward compatibility, modern Ruby code exclusively uses their lowercase counterparts: true, false, and nil. It is strongly recommended to use the lowercase versions.
| Constant Name | Description |
|---|---|
true | The singleton instance of TrueClass. Represents boolean truth. |
false | The singleton instance of FalseClass. Represents boolean falsehood. |
nil | The singleton instance of NilClass. Represents the absence of a value. |
ARGF | An object providing access to a virtual concatenation of files passed as command-line arguments or standard input if no arguments are provided. A synonym for $<. |
ARGV | An array containing the command-line arguments passed to the script. A synonym for $*. |
DATA | An IO stream for reading lines of code that appear after the __END__ directive in a source file. It’s undefined if __END__ is not present. |
ENV | A hash-like object representing the environment variables. You can interact with it much like a standard Ruby Hash (e.g., ENV['PATH']). |
RUBY_PLATFORM | A string identifying the platform on which the Ruby interpreter is running (e.g., "x86_64-linux"). |
RUBY_RELEASE_DATE | A string indicating the release date of the current Ruby interpreter (e.g., "2023-03-30"). |
RUBY_VERSION | A string indicating the version of the current Ruby interpreter (e.g., "3.2.2"). |
STDERR | The standard error output stream. By default, this is where error messages are printed. Corresponds to $stderr. |
STDIN | The standard input stream. By default, this is where gets reads from. Corresponds to $stdin. |
STDOUT | The standard output stream. By default, this is where puts and print write to. Corresponds to $stdout. |
TOPLEVEL_BINDING | A Binding object representing the top-level scope of your program. |
Many of these constants provide insights into the Ruby runtime environment or facilitate interaction with the operating system. For instance, ARGV is essential for writing command-line tools, and ENV is crucial for configuring applications based on the environment.
For a comprehensive list and further details, refer to the official Ruby documentation.