Writing Our First Program
Explore how to write and execute your first Ruby program using the terminal. Understand the role of puts in displaying output, learn basic math operators like addition and exponentiation, and practice running simple Ruby code to build a solid foundation in programming.
We'll cover the following...
A terminal, also known as a console, shell, or command line, is a friend of any Ruby hacker. To run the programs that we’re going to build together, we need a central console, a place from where we can execute these programs: the terminal.
To be precise, this definition of a terminal isn’t one-hundred-percent correct, but it’s often used among programmers. They say “run in the terminal,” but we run all programs using a special software called a “shell.” In other words, we send our instructions to the shell, and the terminal is just a visual wrapper around this shell, where we can configure fonts, colors, copy/paste from our screen, and so on.
To learn about different kinds of terminals and how to set up Ruby on Windows or macOS, check out this lesson. We don’t need to install anything on our platform. All necessary installations are already in place. Here is the command that we will use to check the version of Ruby that was installed:
$ ruby -v
Try out this command in the terminal below.
Our first program
Let’s run Ruby again and write our first program. When we type Ruby (without -v) in the terminal, it will silently wait for our input. Just type puts 1+1 and hit Ctrl+D (sometimes we may need to do this twice):
$ ruby
puts 1+1 (hit Ctrl+D here)
2
$
Explanation
So, what do we see on the screen above? We will command prompt $, and then type Ruby. Ruby is silently running and waiting for our input, and we type puts 1+1, after which we hold the Ctrl key and hit D on our keyboard, which serves as an “end of input” signal. Then, our program executes! The 2 on the screen is the result.
Let’s dive a little bit deeper into what happens above. When we type out Ruby, our shell executes the Ruby interpreter—a special program that is used to read and run our own human-language programs. So, Ruby is just a program that we use to run our programs.
The combination of Ctrl+D—also denoted as ^D, below—will be useful in our software engineering career. This command is just a signal that means “end of my input,” “I’m done here,” or “Now it’s your turn, smart computer”. Our terminal sends a byte with code 4 and Ruby understands that there won’t be any additional keystrokes and it’s time to execute what a user typed.
The puts 1+1 command is our first program! Congratulations on creating it successfully! Unfortunately, we didn’t save it on the disk, because we typed it from our keyboard and it disappeared right after it was executed. But this isn’t a big deal. Because our first program was only eight bytes, we can always type it out again if we want to.
But what exactly does the puts 1+1 command do? Before we answer this question, here is an exercise for us to complete. Try to run just 1+1, without puts, and observe what happens. We don’t see anything on the screen. The calculation is completed successfully, but the result isn’t put back. It’s possible that when we give a long-running task to a computer, such as a set of complex calculations, the result will not be printed because we forgot to type in puts.
In other words, puts just shows the result on the screen. It’s a combination of the words “put” and “string.” There are similar commands in other computer languages to put results on the screen. For example, in BASIC we need to type print, while in C programmers often use printf.
Note: We can also use
printfin Ruby.
But why do we keep puts at the very beginning? We need to sum up two numbers first, and only after that do we need to print results on the screen. Programmers often say “Method (or function) accepts parameters.” In other words, we want to “put string,” and this string is going to be 1+1. This is our parameter. Can you see how we just split this line into two parts?
Ruby offers an alternative syntax for this line, which is a bit more illustrative:
Here is a Ruby program that is used, to sum up two numbers and pass the result of an operation to the puts function as a parameter:
puts(1+1)
Basic maths operators
In mathematics, we calculate the numbers written in the parentheses first, and then do calculations on the rest. The same rule applies when we are working with computer languages.
There are a few basic math operators that we can use in Ruby, such as:
+to add numbers. For example:1 + 1-to subtract numbers. For example:5 - 2/to divide one number by another. For example:120 / 12*to multiply. For example:2 * 5
Without looking at the solution below, try to write a program that calculates the number of milliseconds in one day. How should we approach this problem? How many hours are there in one day? How many minutes are there in one hour? How many seconds are there in one minute? How many milliseconds are there in one second?
Now let’s write a Ruby program to calculate this expression:
* * * *
For the power function in Ruby, we must use **. For example, 5 ** 3 is 5 * 5 * 5 and it equals 125. So, the Ruby program is as follows:
puts 5**5 * 4**4 * 3**3 * 2**2 * 1**1
Try to run this code in the terminal below.
It’s hard to believe, but the result of this operation is exactly the same as the one in the previous example: the number of milliseconds in a day! Both programs will produce the value 86400000. There is no explanation for this. It is just a fun fact. As an exercise, try to run the following program in the terminal below and guess what is going to happen.
puts 60 * 60 * 24 * 1000 == 5**5 * 4**4 * 3**3 * 2**2 * 1**1