w3resource

Interactive Ruby

IRB

IRB stands for “interactive ruby” and is a tool to interactively execute ruby expressions read from the standard input.

IRB stands for "interactive ruby" is a REPL (read–eval–print loop) for programming in the object-oriented scripting language Ruby. The abbreviation irb comes from the filename extension for Ruby is ".rb", although interactive Ruby files do not have an extension of ".irb". The program is launched from a command line and allows the execution of Ruby commands with immediate response, experimenting in real-time. It features command history, line editing capabilities, and job control.

Run in Linux

interactive ruby prompt in linux

Run in Windows

interactive ruby in windows
interactive ruby prompt in windows

Use of irb is easy, when executing irb, prompts are displayed as follows. Then, enter the ruby expression. An input is executed when it is syntactically complete.

prashanta@server:~$ irb
irb(main):001:0> 2+3
=> 5

Syntax:

irb.rb [options] [programfile] [arguments]
 

Command line options

Options Description
-f Suppress read of ~/.irbrc
-m Bc mode (load mathn, fraction or matrix are available)
-d Set $DEBUG to true (same as `ruby -d')
-r load-module Same as `ruby -r'
-I path Specify $LOAD_PATH directory
-U Same as `ruby -U`
-E enc Same as `ruby -E`
-w Same as `ruby -w`
-W[level=2] Same as `ruby -W`
--inspect Use `inspect' for output (default except for bc mode)
--noinspect Don't use inspect for output
--readline Use Readline extension module
--prompt prompt-mode
--prompt-mode prompt-mode
Switch prompt mode. Pre-defined prompt modes are 'default', 'simple', 'xmp' and 'inf-ruby'
--inf-ruby-mode Use prompt appropriate for inf-ruby-mode on emacs. Suppresses--readline.
--simple-prompt Simple prompt mode
--noprompt No prompt mode
--tracer Display trace for each execution of commands.
--back-trace-limit n Display backtrace top n and tail n. The default value is 16.
--irb_debug n Set internal debug level to n (not for popular use)
-v, --version Print the version of irb

Configuration:

IRB reads from ~/.irbrc when it’s invoked.

If ~/.irbrc doesn’t exist, irb will try to read in the following order:

  • .irbrc
  • irb.rc
  • _irbrc
  • $irbrc

The following are alternatives to the command line options. To use them type as follows in an irb session:

IRB.conf[:IRB_NAME]="irb"
IRB.conf[:MATH_MODE]=false
IRB.conf[:INSPECT_MODE]=nil
IRB.conf[:IRB_RC] = nil
IRB.conf[:BACK_TRACE_LIMIT]=16
IRB.conf[:USE_LOADER] = false
IRB.conf[:USE_READLINE] = nil
IRB.conf[:USE_TRACER] = false
IRB.conf[:IGNORE_SIGINT] = true
IRB.conf[:IGNORE_EOF] = false
IRB.conf[:PROMPT_MODE] = :DEFALUT
IRB.conf[:PROMPT] = {...}
IRB.conf[:DEBUG_LEVEL]=0

Auto indentation:

To enable auto-indent mode in irb, add the following to your .irbrc :

IRB.conf[:AUTO_INDENT] = true

To enable autocompletion for irb, add the following to your .irbrc :

require 'irb/completion'

History

By default, irb disables history and will not store any commands you used.

IRB.conf[:SAVE_HISTORY] = 1000

This will now store the last 1000 commands in ~/.irb_history.

See IRB::Context#save_history= for more information.

Previous: Ruby Command Line Options
Next: Ruby Literals



Follow us on Facebook and Twitter for latest update.