irb
Run interactive Ruby shell
TLDR
Start the interactive shell
SYNOPSIS
irb [options] [script_file] [arguments]
PARAMETERS
-f, --raw-string
Forces irb to interpret strings as raw strings, affecting display output.
-m, --nomultiline
Disables multiline input mode, requiring each expression on a single line.
-p, --prompt mode
Sets the prompt mode. mode can be default, simple, inf, or multiline.
-r library
Requires the specified Ruby library before starting the irb session.
-I directory
Adds the specified directory to Ruby's $LOAD_PATH, making files within it discoverable.
-d, --debug
Enables debug mode for the Ruby interpreter (sets $DEBUG = true).
-w, --warn
Enables verbose warnings for the Ruby interpreter (sets $VERBOSE = true).
-v, -V, --version
Displays the irb version information.
-h, --help
Displays a help message with available options.
--noreadline
Prevents irb from using the readline library for enhanced line editing and history.
--irb_debug
Outputs internal irb debugging messages, useful for diagnosing irb itself.
--nolineinfo
Suppresses line number information in backtraces, providing cleaner output.
--echo, --noecho
Controls whether the results of evaluated expressions are echoed back to the console (default is --echo).
--inspect, --noinspect
Controls whether the results are displayed using Object#inspect (default is --inspect).
--show-source-location
Displays the source file and line number for defined methods or constants.
--skip-level n
Skips n levels of irb frames from backtraces, focusing on user code.
--back-trace-limit n
Limits the number of lines displayed in backtraces to n.
--context-mode mode
Sets the initial execution context; common modes include toplevel or binding.
--rc file
Loads a custom configuration file (e.g., .irbrc) instead of the default.
--no-rc
Prevents irb from loading any .irbrc configuration file.
--measure mode, --no-measure
Enables or disables execution time measurement. mode can be off, total, bytecode, or stack.
DESCRIPTION
irb, the Interactive Ruby Shell, is a powerful Read-Eval-Print Loop (REPL) environment for the Ruby programming language. It comes bundled with Ruby and serves as an essential tool for developers to experiment with Ruby code, test snippets, and debug applications interactively. Users can type Ruby expressions directly into the shell, and irb immediately evaluates them, prints the result, and awaits the next input. This iterative feedback loop accelerates learning and development.
Beyond simple expression evaluation, irb supports multiline input, command history, tab completion (when used with readline), and allows loading external Ruby files or libraries. It can be launched with a specific context (e.g., inside a class or object definition) to facilitate testing specific parts of a program. Its flexibility makes it indispensable for quick prototyping, understanding Ruby's behavior, and debugging complex logic without needing to create full scripts.
CAVEATS
irb is a powerful interactive tool, but it's important to note that it runs within a single Ruby process. Global state changes persist between commands, which can sometimes lead to unexpected results if not managed carefully.
While it provides basic error handling and backtraces, complex debugging scenarios might still require a dedicated debugger like ruby-debug or print statements in scripts.
The behavior of some features, like tab completion and command history, can depend on the availability and configuration of the readline library on your system.
CONFIGURATION FILE
irb automatically loads a configuration file named .irbrc from the user's home directory (~/.irbrc) at startup. This file can contain Ruby code to customize the irb environment, such as setting prompt styles, defining helper methods, or requiring commonly used libraries. Users can override this behavior with the --rc or --no-rc options.
INTERACTIVE DEBUGGING
While not a full-fledged debugger, irb can be used for interactive debugging. By calling binding.irb within a Ruby script, execution can be paused at that point, dropping the developer into an irb session. This allows inspecting variables, modifying code on the fly, and stepping through execution, making it a very effective tool for understanding program flow and state.
HISTORY
irb has been an integral part of the Ruby programming language distribution since its early versions, providing a fundamental interactive environment for developers. It was introduced to mirror the concept of interactive shells found in other languages (like Python's IDLE or Lisp's REPL). Over the years, it has evolved with Ruby, incorporating new features, improving user experience with better prompt customization, multiline editing, and enhanced debugging capabilities, solidifying its role as an essential tool for Ruby development and experimentation.