01. Numbers
Now that you've got everything set up, let's write a program! Open your favorite text editor and type the following:
puts 1+2
Save your program (yes, that's a program!) as calc.rb (the .rb is what we usually put at the end of programs written in Ruby). Now run your program by typing ruby calc.rb in the command line. It should have put 3 on your screen. See, programming isn't so hard, is it?
Introduction to puts
So what's going on in that program? I'm sure you can guess what 1+2 does; our program is pretty much the same as:
puts 3
puts simply writes onto the screen whatever comes after it.
Integer and Float
In most programming languages (and Ruby is no exception) numbers without decimal points are called integers, and numbers with decimal points are usually called floating-point numbers, or more simply, floats.
Here are some integers:
5-20599999999999999999999999990
And here are some floats:
54.3210.001-205.38840.0
In practice, most programs don't use floats; only integers. (After all, no one wants to look at 7.4 emails, or browse 1.8 web pages, or listen to 5.24 favorite songs.) Floats are used more often for academic purposes (physics experiments and such) and for 3D graphics. Even most programs that deal with money use integers; they just keep track of the pennies!
Note on Number Formatting:
In English-speaking countries, we typically use a period . as the decimal separator (e.g., 3.14) and a comma , as the thousands separator (e.g., 1,000). Ruby follows this convention for decimal points, using a dot . for floats. However, for thousands separators in code, Ruby allows underscores _ (e.g., 1_000) instead of commas, as commas are used for separating arguments.
Simple Arithmetic
So far, we have everything needed for a simple calculator. (Calculators always use floats, so if you want your computer to act like a calculator, you should also use floats.) For addition and subtraction, we use + and -, as we saw. For multiplication, we use *, and for division we use /. Most keyboards have these keys on the numeric keypad. If you have a smaller keyboard or a laptop, you can use Shift 8 and / (it's on the same key as ?).
Let's try to expand our calc.rb a little. Type the following and then run it.
puts 1.0 + 2.0
puts 2.0 * 3.0
puts 5.0 - 8.0
puts 9.0 / 2.0
This is what the program returns:
3.0
6.0
-3.0
4.5
(The spaces in the program are not important; they just make the code more readable.)
Well, that wasn't too surprising. Now let's try with integers.
puts 1+2
puts 2*3
puts 5-8
puts 9/2
Basically the same thing, right?
3
6
-3
4
Uh... except for that last one! When you do arithmetic with integers, you get integer answers. When your computer doesn't know the "right" answer, it always rounds down. (Of course, 4 is the right answer in integer arithmetic for 9/2; it just might not be what you were expecting.)
Maybe you're wondering what integer division is good for. Well, let's say you're going to the movies, but you only have $9. Here in Portland, you can see a movie at the Bagdad for 2 bucks. How many movies can you see there? 9/2... 4 movies. 4.5 isn't the right answer in this case; they won't let you see half of a movie, or half of you see a whole movie... some things just aren't divisible.
Now experiment with some programs of your own! If you want to write more complex expressions, you can use parentheses. For example:
puts 5 * (12-8) + -15
puts 98 + (59872 / (13*8)) * -52
5
-29802
A Few Things to Try
Write a program that tells you:
- how many hours are in a year?
- how many minutes are in a decade?
- how old are you in seconds?
- how many chocolates do you hope to eat in your life?
Warning: This part of the program might take a while to compute!
Here's a tougher question:
If I am 1232 million seconds old, how old am I in years?
When you're tired of playing with numbers, let's have a look at some letters.