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 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
If ruby is successfully installed you will get a response like the one below giving you the version details of your ruby installation.
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
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):002:0> 2 – 3
And to write that famous “Hello World” program in ruby, all you do is:
irb(main):001:0> puts ‘Hello World’
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]
=> [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.