AI Code

Ruby Language IRB


IRB means "Interactive Ruby Shell". Basically it lets you execute ruby commands in real time (like the normal shell does). IRB is an indispensable tool when dealing with Ruby API. Works as classical rb script. Use it for short and easy commands. One of the nice IRB functions is that when you press tab while typing a method it will give you an advice to what you can use (This is not an IntelliSense)


-fSuppress read of ~/.irbrc
-mBc mode (load mathn, fraction or matrix are available)
-dSet $DEBUG to true (same as `ruby -d')
-r load-moduleSame as `ruby -r'
-I pathSpecify $LOAD_PATH directory
-USame as ruby -U
-E encSame as ruby -E
-wSame as ruby -w
-W[level=2]Same as ruby -W
--inspectUse `inspect' for output (default except for bc mode)
--noinspectDon't use inspect for output
--readlineUse Readline extension module
--noreadlineDon't use Readline extension module
--prompt prompt-modeSwitch prompt mode. Pre-defined prompt modes are default',simple', xmp' andinf-ruby'
--inf-ruby-modeUse prompt appropriate for inf-ruby-mode on emacs. Suppresses --readline.
--simple-promptSimple prompt mode
--nopromptNo prompt mode
--tracerDisplay trace for each execution of commands.
--back-trace-limit nDisplay backtrace top n and tail n. The default value is 16.
--irb_debug nSet internal debug level to n (not for popular use)
-v, --versionPrint the version of irb

Basic Usage

IRB means "Interactive Ruby Shell", letting us execute ruby expressions from the standart input.

To start, type irb into your shell. You can write anything in Ruby, from simple expressions:

$ irb
2.1.4 :001 > 2+2
=> 4

to complex cases like methods:

2.1.4 :001> def method
2.1.4 :002?>   puts "Hello World"
2.1.4 :003?> end
=> :method
2.1.4 :004 > method
Hello World
=> nil

Starting an IRB session inside a Ruby script

As of Ruby 2.4.0, you can start an interactive IRB session inside any Ruby script using these lines:

require 'irb'

This will start an IBR REPL where you will have the expected value for self and you will be able to access all local variables and instance variables that are in scope. Type Ctrl+D or quit in order to resume your Ruby program.

This can be very useful for debugging.

Got any Ruby Language Question?