Skip to content

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 NameDescription
trueThe singleton instance of TrueClass. Represents boolean truth.
falseThe singleton instance of FalseClass. Represents boolean falsehood.
nilThe singleton instance of NilClass. Represents the absence of a value.
ARGFAn 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 $<.
ARGVAn array containing the command-line arguments passed to the script. A synonym for $*.
DATAAn 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.
ENVA hash-like object representing the environment variables. You can interact with it much like a standard Ruby Hash (e.g., ENV['PATH']).
RUBY_PLATFORMA string identifying the platform on which the Ruby interpreter is running (e.g., "x86_64-linux").
RUBY_RELEASE_DATEA string indicating the release date of the current Ruby interpreter (e.g., "2023-03-30").
RUBY_VERSIONA string indicating the version of the current Ruby interpreter (e.g., "3.2.2").
STDERRThe standard error output stream. By default, this is where error messages are printed. Corresponds to $stderr.
STDINThe standard input stream. By default, this is where gets reads from. Corresponds to $stdin.
STDOUTThe standard output stream. By default, this is where puts and print write to. Corresponds to $stdout.
TOPLEVEL_BINDINGA 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.