Join Kevin Skoglund for an in-depth discussion in this video Using Ruby, part of Ruby Essential Training.
Now that we have everything we need for programming in Ruby installed on our computers, we are ready to take a look at how to use Ruby. There are three different ways that I want us to look at. The first is going to be a single command. The second will be using a Ruby file, actually saving a file that has Ruby code in it. The third will be IRB, the Interactive Ruby Shell. Now, we are going to look at the first two in this movie and we will spend some more time with IRB in the next movie. All three of these ways of interacting with Ruby are going to require that we use our command line. So here I am inside my command line program on the Mac, which is Terminal.
If you are on a PC, you will want to use the Command Prompt application that we talked about in the Installation section. But once you come up in there, you should be able to type Ruby and then -v, and it will let you know that you have Ruby installed and report what version you have. Again, don't worry if your version is not the same as mine. We talked about in the Installation section that most versions are going to work just fine, as long as we have Ruby installed. Running a single command is as easy as typing Ruby and then -e, to let it know they we are going to use a single command version of Ruby, -e. And then a single quote and our Ruby command, puts 123, and another single quote.
That's the command that we want to execute, the single line of Ruby. So now we have learned our first Ruby command, puts, and that's actually 123, not 1, 2, 3, even though I said it 1, 2, 3. We will hit Return, and you will see what it does. puts actually output 123. It put it to whatever our standard out was, which in this case was just the Terminal window. Before we move on, let's go ahead and take a look at our second Ruby command, which is going to be print. It does something very similar to puts. Now, notice the difference. Notice where print went. print is right before my command prompt. The difference is that there is no line return after print.
So puts outputs whatever you asked it to and adds a line return at the end. print just outputs whatever you have asked it to do, and does not put that line return. So now we know two bits of Ruby, puts and print. Let's take a look at how to do this with the file. In order to do that, we will need our text editor. I am going to open up TextMate, which is mine, and I am just going to put a simple Ruby command in here, puts 123, and then right after it let's put print 456 and then puts 789. So we are going to Save this and we are going to call this file simple_file, and the ending is going to be .rb, to let it know that it's a Ruby file that ought to be run with Ruby. .rb is going to be the extension.
We are always going to want to use that. You can save it anywhere you like. This is not like a web application where we need to put these in a certain sites directory or something like that. These can be run absolutely anywhere on your computer, but you want to put it someplace that's easy for you to find. I am going to go ahead and just put it on my Desktop. You can put it anywhere, in your Documents folder. It doesn't really matter, as long as you are able to find it from the command line. So I am going to save it there. So now it's saved. You see the code coloring took effect. That's the code coloring inside TextMate. Yours may be different, that's fine. If I come back over here to the command line now, I will need to navigate to where that file is.
So we can see a list of the files that are in the current directory using ls, if we are on a Mac, or dir, if you are on a PC, and that will tell you what's inside that directory. We can change directories by using the cd command. So I am going to do cd into my Desktop folder. Hitting Tab allowed me to auto complete that name and then when I switch to the Desktop, now if I do an ls, you will see that I see that simple_file there. So once we are in the same directory as the file, then we can simply run Ruby, and then the filename, and once again the Tab lets me auto complete that name.
So ruby simple_file, let's run it, and look what it output, 123, and then 456789. Now, notice if we go back, either we used puts here, print here, and puts here, and you will see the effect of not having that line return between the 6 and the 7. So that's all there is to being able to run a Ruby file. Now, if we hadn't navigated into Desktop, we could still do it just by providing the full path to the file. So if I go backwards a directory, we do that with the ..cd, and then dot dot goes back one, into the parent directory, and now I could just as easily say Ruby, and then Desktop/simple_file.rb, and that will also run that same file.
So we just need to either be in the same folder, or we need to specify the full path on how we can get there. Before we move on, I just want to point out a couple of things about the file that we are working with here. First of all is that Ruby is whitespace independent. So it doesn't matter whether you put some extra whitespaces here, some extra ones here, it's still going to sort that out. So it's not going to really matter where those are. If we save this, come back here and we run the file again, you see we get the same results. The second thing I want to point out to you is the way we do comments.
A comment in Ruby is simply the pound sign, also sometimes called the hash or the sharp. So we will put that in front of any of our comments. So then I will say print does not add a line return, and now we have a comment. Something that we can comment in our code and will not get executed. Notice also that we are not using any kind of semicolons or anything like that at the end of our lines. We just simply have a simple line return, and Ruby then knows, hey, we are ready to move on to something else. Print got everything that it needed on this line, so it's not waiting for more data to come in.
So that must mean this is a new command, starting on the next line. So let's save it, and let's just run it to reassure ourselves that it all still works. Now, running a single command in Ruby isn't going to be useful for us that often. Running a file is going to be very useful for us, especially if we have lots and lots of code. We are not going to want to retype it every time. We will want to put it in a file, save it, and run it. But there is also Interactive Ruby Shell, which is going to be very useful for us, especially at the beginning, to try things out quickly. Once we actually start writing a program, we will put it in a file.
For now, let's move over and look at 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