Use irb3.2 to Test Ruby Code Without a Project
You will finish with a small, repeatable IRB workflow for checking Ruby expressions, loading a library, and running a Ruby file when you do not need a full project. The examples use the installed Ruby 3.2.3 and IRB 1.6.2 on this machine. They do not install gems or change system configuration.
The route
Jump straight to the step you need, or tick off Done means at the end.
Allow about ten minutes. You need a shell and the ruby package. No command in this guide needs sudo. IRB evaluates Ruby code in your user account, so treat pasted code as executable, especially code copied from an issue, chat or web page.
1. Check the installed command
Start by confirming which executable will run. This is an ordinary, read-only check:
$ command -v irb
/usr/bin/irb
$ irb --version
irb 1.6.2 (2022-12-13)
$ ruby --version
ruby 3.2.3 (2024-01-18 revision 52bb2ac0a6) [x86_64-linux-gnu]
The irb and irb3.2 commands installed here report the same IRB version. The irb3.2 name identifies the Ruby 3.2 package, while irb is the convenient alias. Use the versioned name when a script, note or runbook must make that choice obvious.
Checkpoint: if command -v irb finds nothing, stop here. Install or repair the Ruby package through your normal distribution process rather than downloading a replacement into the current directory.
2. Evaluate one expression
Run irb with no arguments for an interactive session. Type an expression, press Enter, and IRB prints its result:
$ irb
irb(main):001:0> 1 + 1
=> 2
irb(main):002:0> "ruby".upcase
=> "RUBY"
irb(main):003:0> exit
$
IRB waits when the input is not syntactically complete. That is why a method definition can span several prompts:
irb(main):001:0> def double(value)
irb(main):002:1> value * 2
irb(main):003:1> end
=> :double
irb(main):004:0> double(21)
=> 42
The number after the colon is the nesting level. A prompt ending in > on a new line is usually a request for more Ruby, not a hung terminal. Enter the missing end, closing bracket or quote. Use exit, quit or press Ctrl-D to leave an ordinary session.
3. Use a predictable non-interactive check
For a command that belongs in a shell script or a copy-and-paste check, pipe Ruby source to IRB and suppress prompts:
$ printf '%s\n' '1 + 1' 'exit' | irb --noprompt
Switch to inspect mode.
1 + 1
2
exit
The installed IRB 1.6.2 prints the input lines and the short mode message in this piped form. The useful result is 2. If you need clean machine-readable output, use ruby with a small script instead; IRB is an interactive developer tool, not a stable output protocol.
For a human session with less visual noise, --simple-prompt changes the prompt shape. --noprompt removes prompts, but it does not turn IRB into a general Ruby script runner or remove Ruby's own output and errors.
4. Load a standard library before evaluating code
Use -r to require a library before the session starts. This example loads Ruby's JSON library and then calls it:
$ irb -r json
irb(main):001:0> JSON.generate({ok: true})
=> "{\"ok\":true}"
irb(main):002:0> exit
-r json is equivalent to requiring json before your first expression. It searches Ruby's load path, so a missing library produces a load error rather than silently doing nothing. To test a local library directory, use -I DIRECTORY:
$ irb -I /path/to/lib -r example_library
Replace the path and library name with values that exist on your machine. Keep paths quoted when they contain spaces. Do not add a directory merely to make an unexplained load error disappear: inspect what code it contains first.
5. Run a Ruby file when the REPL is not the right tool
IRB accepts a program file followed by arguments. This is useful when you want IRB's environment or output handling while checking an existing script:
$ irb --noprompt /path/to/check.rb first-argument second-argument
The file path and arguments are placeholders. Confirm the file before running it:
$ test -r /path/to/check.rb && printf 'readable: %s\n' /path/to/check.rb
readable: /path/to/check.rb
Do not use IRB as a safety wrapper for an untrusted script. The file can read your files, use your network access and change data with the same permissions as your account. If you only need to execute a script, ruby /path/to/check.rb expresses that intent more directly.
6. Control startup configuration
By default, IRB may read personal configuration from ~/.irbrc. If IRBRC is set, it names the configuration file instead. With XDG_CONFIG_HOME set and no IRBRC, IRB also recognises $XDG_CONFIG_HOME/irb/irbrc. Configuration can change prompts, helpers and startup behaviour, so it matters when a result must be reproducible.
Use -f to suppress the personal startup file for a clean diagnostic session:
$ irb -f --simple-prompt
>> 1 + 1
=> 2
>> exit
This does not delete or modify the configuration. It only skips loading it for that process. If two users see different prompts or different methods at startup, compare IRBRC, XDG_CONFIG_HOME and the relevant file before comparing Ruby code.
7. Inspect errors without changing anything
A syntax or runtime error is reported inside the session and normally leaves IRB available for another expression:
irb(main):001:0> Integer("not-a-number")
ArgumentError: invalid value for Integer(): "not-a-number"
irb(main):002:0> 6 * 7
=> 42
If the diagnostic is too short, --verbose shows more detail and --tracer displays a trace for each command. --back-trace-limit N limits the top and tail of a backtrace; the installed manpage gives a default of 16. These options affect diagnostics, not the code's permissions or behaviour.
If a command unexpectedly starts with different settings, retry with -f. If a required library is missing, verify the name and load path before trying a different Ruby installation. Elevated privileges will not repair a Ruby load path and can create root-owned files if the code writes anything.
Done means
- You confirmed the selected
irbexecutable and its Ruby/IRB versions. - You evaluated an expression and recognised the multiline prompt.
- You know when to use
-r,-I,-fand--noprompt. - You can separate an IRB display problem from a Ruby error.
- You checked local files and configuration before running unfamiliar code.
- You left system files, gems and persistent configuration unchanged.