Join Kevin Skoglund for an in-depth discussion in this video Interactive Ruby Shell (IRB), part of Ruby Essential Training.
Now that we have seen the first two ways that we can interact with Ruby, either using a single Ruby command, or creating a file with all of our Ruby code in it, now let's take a deeper look at the Interactive Ruby Shell, or more simply called IRB. IRB is going to be a command line interface to Ruby. Now, why that's really great is because it allows us to really interact with our code. We can see what's happening in real-time. It's a lot like a calculator. We can type in a couple of values, get a result, type in a few more values, get a different result, and then we can compare and see how things are working. It's a great place to test our code.
So even when we are really advanced and we are writing all of our code in a very complex system of files that are all connected together, IRB is going to be a great way to quickly just pop open Ruby, try something out, test our theory, and then go back and actually add it to our code. So let's take a look. The way that we will access IRB is going to be from the command line. So when I say command line on a Mac, I am talking about the Terminal application that's inside your Utilities folder, and on a PC, that will be the Command Prompt application that's inside your Accessories folder. We saw how to find those earlier.
To run it, we simply type irb at the command line. IRB is a program that's installed when we install Ruby. So we have irb, now I am inside, and you see I have a prompt there, letting me know that I am in IRB, it's waiting for a command. Now, IRB works a lot like a calculator. We can do 1+1. Comes back and tells us that it's 2. Or we can say 4+5, 9. Let's try something a little more complex, 3 * 50, or let's try 45/9.
And last of all, let's just do some subtraction, 100-10. So you see that it just works like a calculator and just returns these values to us. Now, let's try the commands that we used earlier. We used puts, if you remember, 123. Now, this brings up an important point about IRB that I want you to pay close attention to. Notice that it returned two things to me. The first thing is 123, and then the arrow notation is next to nil. Just like we had the arrow notation up here, next to 90. So what's going on here? Puts is outputting it to IRB, just the same way that it outputted to our command line before, when we were running it from either the file or from a single line.
This is the return value that comes back. So this is both the output that's happening and there is the return value and the return value of puts is always nil. The return value of these calculator operations that we were doing up here is the value that we are looking for. It's not actually outputting it; instead it's doing the calculation and giving us a return value. So this is an important difference. Let's try this, puts 1+1, and notice what it gives us. See, we get the 2 that's being put, and the return value is nil and that's distinctly different from just doing 1+1.
So keep this in mind, because when we are doing a puts, which we will be doing a lot, we are going to be outputting something to the command line, so that we can see it, but the return value may be simply nil. Now, it's worth mentioning that nil means nothing. It's not 0. It's actually less than 0. It's nothing. It doesn't exist. It's no value at all. In a lot of languages this is just referred to as null. In Ruby it's going to be nil all the time. The next thing I want to point out to you about IRB is that you should be able to use your up arrows to get to old commands that you have issued before, and you can just hit return to execute them again, or the down arrow.
Once you go up a few, you can come back down a few the same way. That can be really handy for finding an old command that you typed, especially if it's a long line, and just allowing it to be reentered another time. The other thing I want to introduce is a bit of the Ruby syntax. I am going to put a double quote, followed by Hello, and then a dot, and the word reverse. So now we are actually writing some Ruby code. We will talk a little bit more about what Hello is later. For now just know that it's an object. We have told that object to reverse itself, and this is the thing I want you to notice is the dot notation here, and the dot notation tells it to reverse.
Then in order to get out of IRB, we simply type quit, and that takes us back to our command prompt. Now, there is one other thing I want to show you about IRB, which is if we do IRB and then --simple-prompt, there we go, --simple-prompt, then we get a simpler prompt, 1+1. Everything works exactly the same; we just have a much shorter prompt line at the end of each one. Some people like the longer one. It gives you line numbers and everything. The other one doesn't. It just sort of gets all that out of your way. So it's up to you.
You may already be configured to use the simple prompt. If so, that's fine. But this will allow us to have either the simple prompt or the complex prompt, depending on your preference. I will type quit one last time, and that's all you need to know to get started working with IRB.
- Using Ruby in the Interactive Ruby Shell and in standalone scripts
- Learning to write custom code blocks to find, merge, and sort
- Using modules for namespacing or as mix-ins
- Reading from and writing to files
- Creating a full Ruby project from start to finish