The Top-Level Environment

When the Ruby interpreter starts, a number of classes, modules, constants, and global variables and global functions are defined and available for use by programs. The subsections that follow list these predefined features.

Predefined Modules and Classes

When the Ruby 1.8 interpreter starts, the following modules are defined:

Comparable      FileTest        Marshal         Precision
Enumerable      GC              Math            Process
Errno           Kernel          ObjectSpace     Signal

These classes are defined on startup:

Array           File            Method          String
Bignum          Fixnum          Module          Struct
Binding         Float           NilClass        Symbol
Class           Hash            Numeric         Thread
Continuation    IO              Object          ThreadGroup
Data            Integer         Proc            Time
Dir             MatchData       Range           TrueClass
FalseClass      MatchingData    Regexp          UnboundMethod

The following exception classes are also defined:

ArgumentError           NameError               SignalException
EOFError                NoMemoryError           StandardError
Exception               NoMethodError           SyntaxError
FloatDomainError        NotImplementedError     SystemCallError
IOError                 RangeError              SystemExit
IndexError              RegexpError             SystemStackError
Interrupt               RuntimeError            ThreadError
LoadError               ScriptError             TypeError
LocalJumpError          SecurityError           ZeroDivisionError

Ruby 1.9 adds the following modules, classes, and exceptions:

BasicObject     FiberError      Mutex           VM
Fiber           KeyError        StopIteration

You can check the predefined modules, classes, and exceptions in your implementation with code like this:

# Print all modules (excluding classes) puts Module.constants.sort.select {|x| eval(x.to_s).instance_of? Module} # Print all classes (excluding exceptions) ...

Get The Ruby Programming Language now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.