Playing in IRB

Nikesh
Makers
Published in
3 min readMay 6, 2016

--

Now that you’ve got Ruby installed, let’s start coding!

With your terminal open, type the following command:

irb

This will open up the interactive ruby console and you should see a prompt like this: “>”. With irb, you can type ruby code that’s instantly run in your terminal.

The ‘irb’ is a Ruby REPL (read-evaluate-print loop) because it waits for you to enter a Ruby expression, reads it, evaluates it and prints the result of the evaluation back to the terminal. So, if we put 2 + 2 into irb, it will evaluate it using Ruby and show the result: 4.

> 2 + 2 => 4

Irb is very useful for playing with Ruby. Whenever you think “Hey, what would this bit of code do?”, the answer is not to google it, read about it & try to memorise what it does…

It’s much easier, and you’ll learn a lot more, if you play with it in irb and see if it does what you think it should do. Learning through exploration and play is vastly more effective than memorising other peoples words.

Sometimes you’ll be surprised by what you find when you play in irb. For example, what will this code do?

5 / 2

You may expect that it will evaluate to 2.5 but you’ll be wrong.

> 5 / 2 => 2

This is not a mistake, it’s a pecularity of Ruby language. We’ll discuss why it evaluates to 2 instead of 2.5 shortly.

Don’t assume that irb is only good for relatively simple snippets of code. You can put something really sophisticated into irb as well (and you will, as you learn more about Ruby).

In particular, you can enter blocks of code that span multiple lines. If a line cannot be evaluated because you haven’t finished entering it yet, irb will wait for you to continue on the next line. Try hitting ‘enter’ after the plus sign, for example.

> 2 + > 2 => 4

Since you pressed enter after the plus sign (line 3), it’s obvious to Ruby that you haven’t finished entering the expression to evaluate, so it will wait until you finish it on the line 4 and only then print the result (“4”).

This also happens if you enter text into irb. A piece of text is called a “string” in Ruby (because it’s a “string” of characters) and must be enclosed in quotes. If we enter a string into irb, it is just returned back to us.

> "Hello, Makers!" => "Hello, Makers!"

If, however, we forget to close the string by omitting the double quote, irb will wait until you enter it because it will assume you’re still entering the string. This is a very common source of confusion among beginners because it will look like the irb isn’t responding. This is what’s happening here:

:001> 2 + 2 => 4 
:002 > "Hello, Makers
:002"> I forgot to close the quote
:002"> so even if I press 'enter', it doesn't do anything
:002"> 2+2
:002"> it doesn't print '4', because we're still typing
:002"> the string that began with 'Hello, Makers'
:002"> Ok, let's stop this nonsense and close the quote"
=> "Hello, Makers\nI forgot to close the quote\nso even if I press 'enter', it doesn't do anything\n2+2\nit doesn't print '4', because we're still typing\nthe string that began with 'Hello, Makers'\nOk, let's stop this nonsense and close the quote"

Note that lines 7 to 12 (when the string was still not “closed”), there was a quote character before the prompt (:002">) instead of a space (:001 >). This was an indication that irb is waiting for the closing quote.

When you want to quit irb, just type exit.

Demo

Here’s a short video that shows you how to use IRB

Advanced IRB

You can control what happens when you start irb using a .irbrc file placed in your home directory, e.g.

Dir['./lib/*.rb'].each { |f| require f }

will autoload ruby files from your lib directory.

Now that you’ve see what can be done, stop focusing on memorisation of syntax!

Remember to “play in IRB”, use it to try out ideas for code solutions and to actually test what your code does before you suggest it as a solution to a challenge.

Originally published at blog.makersacademy.com on May 6, 2016.

--

--