This is a copy of the handout I’ll be giving to students for my upcoming class at the Center on Halsted, creatively titled “Introduction to Programming.” It borrows structure rather liberally from Chris Pine’s Learn to Program, but the content is my own.
Getting Started with Ruby
Welcome to Programming
Computer programming is the skill that allows you to make computers do what you want. Computers are excellent at menial, repetitive tasks that require little oversight and no creativity: but you can also use them to make amazing websites, games, and applications. Whether it’s a simple program to make your day-to-day life easier, or a complicated application that gives you full-time employment, computer programming is invaluable professionally and also rewarding personally. Even better, it’s lots of fun!
Many people are intimidated by the idea of learning computer programming – they’re worried you need complicated math or lots of formal training to do well. Nothing could be further from the truth. Computer programming can be learned by anyone and is very much learning a new language. All you need to succeed at computer programming is familiarity with computers, a willingness to learn, and a computer to do the programming on.
You’ll be learning computer programming through the computer language Ruby. Ruby was created with two goals in mind: productivity and fun. While easy to learn, it’s also an extremely powerful programming language widely used in the professional world. Some of the most popular websites on the Internet were created with Ruby – for example, Github, Hulu, and Twitter are all implemented in Ruby.
To learn Ruby, we’ll be using an online tool that allows us to quickly and easily try Ruby, called repl.it. In our first exercise, we’ll go to repl.it and type in an example expression.
Open up your web browser.
In the address bar, type
repl.it, and press enter.
Select Ruby (beta). A new window opens; on the right half of the screen you should see some information and a command prompt, and on the left half a blank white space for typing.
The right half of the screen is the Ruby console. Type
1 + 1into it and press enter.
The output of your command is returned to you immediately. Congratulations, you’re a computer programmer!
Computers are really excellent at math. In this exercise, we’ll learn how to do math in Ruby.
2 * 3.
*is the multiplication operator.
9 / 3.
/is the division operator.
2+2. You don’t need spaces around the operator; in fact, Ruby doesn’t care how many spaces appear in a statement.
-9 - 3. By putting a minus in front of a number, it becomes negative.
9 / 0. This is your first ever error message in Ruby. The kind of error appears before the colon (ZeroDivisionError, in this case); some helpful error text appears afterwards. Obviously, this error means you can’t divide by zero.
5 / 2. It might surprise you that the result is 2 instead of 2.5; what’s going on here?
In Ruby, there are two kinds of numbers. There are integers – that is, whole numbers, whether positive or negative – and there are floats. Floats (short for “floating-point numbers”) are numbers with any kind of decimal place, even if that decimal place is zero.
5.0 are all floats.
Most of the time, you’ll be using integers. This is because most people (and thus most programs) deal in whole things: you don’t receive 4.3 emails, or view 1.8 webpages, or listen to 5.24 of a song. Floats are used when you’re doing more complicated mathematics, for example graphics or physics.
In this exercise, let’s do some math on floats.
2.0 * 3.0. Of course, the answer
6.0is the same as the answer for integer math,
5.0 / 2.0. Now that answer looks a lot more like what we’d expect!
-9.0 + 8.0. Everything you would expect to work in integers works fine on floats.
(598.0 / (92.0 - 90.0)) * 43.0 + 2.0. Even complicated math is easy with computers!
Numbers are exciting, but people generally use words a lot more than they do numbers. We refer to letters, words, and sentences in Ruby as strings. Strings in Ruby are always encapsulated by either single or double-quotes. In this exercise, we’ll write a few strings to see how they work.
'Hello.'. Notice that when Ruby returns a string to us, it’s always in double quotes, even if we typed it in single quotes.
"This is a big fancy string.". Strings can be of any length and can contain any characters: spaces or punctuation included.
'2 is my favorite number'. Numbers that are in strings are just more words.
'Well isn't this nice.'. Oh no!
"She said, "This is foolish!""What’s happening?
You’ll get a syntax error for the last two statements above. In both cases, Ruby is expecting a single quote or double quote to end the string, but instead we’re using one inside the actual string itself. Ruby is only a computer language. Unlike a human reading either of those sentences, it can’t tell which quotation marks are part of the sentence and which are intended to start and end the string; instead it gives us a syntax error.
Strings are fun to type, but they’re even more fun to actually use! Let’s do an exercise that shows us the power of strings.
'Hello' * 3. Whoops, we probably wanted to put a space in there. Let’s try it again with
'Hello ' * 3. There, isn’t that much better?
'Message in ' + 'a bottle.'Again, space is important.
'12' * 3. Is this different than what you’d expect?
'12' + '12' + '12'. How about that?
The last two won’t actually do math, like you might expect. Remember
'12' is a string:
12 is an integer! So
'12' is no different from
'love' – it’s a word to Ruby, not a number. If you try to use strings as numbers, Ruby won’t understand what you mean. Here’s a quick exercise to illustrate this point.
'14' + 14.
3 * 'bacon'.
If you think about it, both of these errors make sense. What would it mean to add 14 to the word “fourteen?” How do you multiply three by bacon? Remember, computers can only do what you tell them. If you tell them to do things that don’t make any sense, they’ll return errors. Still, wouldn’t it be nice to be able to add
14 and get 28, like we’d expect?
'My favorite number is 3' all have in common? In Ruby, they’re all objects. Ruby objects are very similar to real-world objects. Objects in Ruby can do things, just like objects you interact with every day. A car can drive, a cat can meow, and a person can smile. Objects in Ruby can do things as well; and the things that they can do are called methods.
In Ruby, you make an object perform a method with a period, and then the name of the method you want to call. So if you could use Ruby on your car, you would use
car.drive to drive it, and if your cat wanted to meow, it would do
cat.meow. Let’s try a quick exercise to see how easy it is to use methods.
'Hello'.reverse. reverse is a method for strings: it reverses the string for you. How helpful! In this example, ‘Hello’ is the object. The period instructs Ruby that a method is coming up next, and reverse is the name of the method.
1.odd?. Ruby tells you
true, which obviously means that 1 is an odd number.
1.zero?. One is not zero, so Ruby returns
'Sentence!'.length. Length is a string method that returns how many characters are in that string.
'I want to yell this'.upcase. Upcase transforms a string into all capitals.
There are dozens of possible methods for strings and numbers. Learning Ruby is very much like learning a new language – while you will learn the grammar and the most commonly-used words in this class, you’ll eventually want to grab a dictionary and look up more words yourself. Ruby’s documentation contains a simple, complete, easy-to-read list of methods and is free online. There are instructions for finding it at the end of this handout.
Ruby tries to be as sensible as possible. Methods are named to be unsurprising and predictable. For example, if you know about
upcase, you can guess that there’s a similar method called
downcase that does exactly the opposite – that is, it would convert all letters into lowercase. And you’d be right!
Because Ruby tries very hard to be unsurprising, there are many methods with the same name between different kinds of objects. These methods generally do the same sorts of things. For example, strings and numbers both have a method that might look very familiar to you: the method
+! Yes, you were already using methods without even realizing what they were. Try a quick exercise to prove it.
The plus method is special. You don’t need a period before it, but on almost all other methods, you do. It also acts differently for strings and numbers. On strings,
+ concatenates strings together. But on numbers, it actually adds them.
You might be wondering: what’s with the parenthesis containing the 1 and “one”? That’s an argument. An argument tells a method additional information it needs in order to perform its function. When you provide an argument (or arguments) to a method, you do so in a parenthesis after the method name itself. Let’s do some quick exercises to see how methods accept arguments.
'hello'.delete('l'). The argument to the
deletemethod tells it which letter it should delete from the string.
'space'.center(20). The argument to the
centermethod tells the string how many spaces it should be centered in.
'space'.center(20, '-'). In Ruby, some methods accept more than one argument: each additional argument is separated with a comma. The second argument to
centertells it what character to pad the string with. In this case, it gives us a lot of dashes.
Unfortunately, not all methods are present on every object. Try this exercise to get a new, exciting Ruby error that you’ll probably see a lot of: the
reverseis a method for strings only.
"Hello".odd?Drat. Looks like
odd?is only for numbers.
NoMethodError, Ruby is letting you know that a method you tried to call on an object didn’t exist. You might have the wrong kind of object, or maybe you mistyped the method name. Whatever the case, this is Ruby’s nice way of telling you that you messed up somewhere.
Remember earlier how we thought it’d be nice if you could add
14 together to get 28? We now have all the ingredients to make this happen. Ruby has methods that convert one kind of object into another kind of object. In this exercise, we’ll do some exciting conversions!
1.to_s. to_s means “to string.” Notice that the result comes back with quotation marks – this isn’t the number 1 anymore, but instead the word “1”!
1.to_f. to_f means “to float.” The result is the number 1 as a float – that is, 1.0.
2.3.to_i. to_i means “to integer.” Because integers can only be whole numbers, the trailing .3 is dropped, converting the number to 2.
'3'.to_i. The word “3” is now the number 3.
'14'.to_i + 14. Pretty convenient, huh?
The result of that last one might surprise you. Ruby tries very hard to perform conversions sensibly, but when it can’t convert a string, it’ll return zero instead. So be careful of what you’re trying to convert, or else you might get zero back instead.
Now that we’ve covered the basics of Ruby, we’ll put what we’ve learned into action. Using variables, we’ll create our first Ruby programs, and finally make the computer work for us!