Exercise 1: A Good First Program

You should have spent a good amount of time in Exercise 0 learning how to install a text editor, run the text editor, run the terminal, and work with both of them. If you haven't done that, then do not go on. You will not have a good time. This is the only time I'll start an exercise with a warning that you should not skip or get ahead of yourself.

Type the following text into a single file named ex1.rb. Ruby works best with files ending in .rb.

1
2
3
4
5
6
7
puts "Hello World!"
puts "Hello Again"
puts "I like typing this."
puts "This is fun."
puts "Yay! Printing."
puts "I'd much rather you 'not'."
puts 'I "said" do not touch this.'

Your Atom text editor should look something like this on all platforms:

_static/ruby/osx_atom.png

Don't worry if your editor doesn't look exactly the same; it should be close though. You may have a slightly different window header, maybe slightly different colors, and the left side of your Atom window won't say "zedshaw" but will instead show the directory you used for saving your files. All of those differences are fine.

When you create this file, keep in mind these points:

  1. I did not type the line numbers on the left. Those are printed in the book so I can talk about specific lines by saying, "See line 5..." You do not type line numbers into Ruby scripts.
  2. I have the puts at the beginning of the line, and it looks exactly the same as what I have in ex1.rb. Exactly means exactly, not kind of sort of the same. Every single character has to match for it to work. Color doesn't matter, only the characters you type.

In OSX Terminal or (maybe) Linux run the file by typing:

ruby ex1.rb

If you did it right then you should see the same output as I in the What You Should See section of this exercise. If not, you have done something wrong. No, the computer is not wrong.

What You Should See

On macOS in the Terminal you should see this:

_static/ruby/osx_terminal_ex1.png

On Windows in PowerShell you should see this:

_static/ruby/win_powershell_ex1.png

You may see different names, before the ruby ex1.rb command, but the important part is that you type the command and see the output is the same as mine.

If you have an error it will look like this:

> ruby ex1.rb
ex1.rb:3: syntax error, unexpected tCONSTANT, expecting $end
puts "I like typing this."

It's important that you can read these error messages because you will be making many of these mistakes. Even I make many of these mistakes. Let's look at this line by line.

  1. We ran our command in the Terminal to run the ex1.rb script.
  2. Ruby tells us that the file ex1.rb has an error on line 3. The type of error is "syntax error", and then some programmer jargon you can usually ignore.
  3. It prints the offending line of code for us to see it.

Study Drills

The Study Drills contain things you should try to do. If you can't, skip it and come back later.

For this exercise, try these things:

  1. Make your script print another line.
  2. Make your script print only one of the lines.
  3. Put a # (octothorpe) character at the beginning of a line. What did it do? Try to find out what this character does.

From now on, I won't explain how each exercise works unless an exercise is different.

Note

An "octothorpe" is also called a "pound", "hash", "mesh", or any number of names. Pick the one that makes you chill out.

Common Student Questions

These are actual questions that real students have asked when doing this exercise.

How do you get colors in your editor?
Save your file first as a .rb file, such as ex1.rb. Then you'll have color when you type.
I get ruby: No such file or directory -- ex1.rb (LoadError).
You need to be in the same directory as the file you created. Make sure you use the cd command to go there first. For example, if you saved your file in lrthw/ex1.rb, then you would do cd lrthw/ before trying to run ruby ex1.rb. If you don't know what any of that means, then go through Appendix A.

Buy DRM-Free

When you buy directly from the author, Zed A. Shaw, you'll get a professional quality PDF and hours of HD Video, all DRM-free and yours to download.

$29.99

Buy Directly From The Author

Or, you can read Learn Ruby the Hard Way for free right here, video lectures not included.