IRB (Interactive Ruby)

The best way to start learning how ruby works, is to play around in the Interactive Ruby Shell. So how do we get irb?

If you are on Windows visit RubyInstaller and you are able to download the latest version of Ruby. If you are a Mac user walks you through installing ruby.

If you are an absolute beginner, it is advised to download Ruby 1.9.3 installers. At the time of this post I am using ruby 1.9.3.

Once you have installed ruby on your machine, open your command prompt and type

C:\>ruby –version

If ruby is successfully installed you will get a response like the one below giving you the version details of your ruby installation.

C:\>ruby –version

ruby 1.9.3p392 (2013-02-22) [i386-mingw32]

To invoke irb, at the command prompt type irb and hit return



Then you can start experimenting. For example, if you want to add two numbers in ruby, all you do is


irb(main):001:0> 1 + 2

=> 3

As you can see the second line shows the value of the last expression evaluated by irb.  Of course you can experiment with various other methods like this

irb(main):001:0> 2*3

=> 6

irb(main):002:0> 2 – 3

=> -1

irb(main):003:0> 2%3

=> 2

irb(main):004:0> 2**2

=> 4

And to write that famous “Hello World” program in ruby, all you do is:

irb(main):001:0> puts ‘Hello World’

Hello World

=> nil


Very Neat!

I personally like irb because it’s such a great quick check tool. For example, I wanted to see how an array object would behave when I applied a method called “shuffle”. So I typed it in and irb showed me:

irb(main):005:0> p = [1, 2, 3, 4, 5, 6, 7]

=> [1, 2, 3, 4, 5, 6, 7]

irb(main):006:0> p.shuffle

=> [6, 5, 7, 3, 2, 1, 4]


The method did exactly what I was hoping it would and I was able to easily check it out in irb before I implemented it in my algorithm.