The basics

Reading from the command line

Use the irb to try out the examples.

Don’t forget to type!

Writing to the console

print is similar to puts but it doesn’t add a new line to the end of the output.

print "x + y ="

Reading from the console

To read input from the console:

  input = gets

To read input without the new line use chomp

  input = gets.chomp


A range is an interval with a beginning and an end.

To get the numbers between 1 until 5.


To get all the numbers from 1 up to 5, excluding 5.


to_a returns an array representation of an object. In the above case, it converts a range to an array


Random is an interface to a number generator. The rand() method takes in an integer value max. It returns an integer greater than zero and less than max. You can also use a range as a parameter.


Exercise 1: Numbers game!

In this exercise we’ll create a small game to help us practise maths.

First, create an empty file called numbers.rb.

To execute the file, run the following:

ruby numbers.rb

In this exercise we want to generate two random numbers x and y using Random.rand(). We will output the numbers to the console and read in the answer.

print "#{x} + #{y} = "

answer = gets.to_i

to_i returns an integer representation of an object.

if the answer is correct, output Right!. If the answer is incorrect, output Wrong :(.

Use an if .. else .. statement

Now we also want to limit the generated number to be between 1 and 10. To do that, we can pass in a range as a parameter to Random.rand().

Extending the game

Let’s add a loop, so that the game is repeated 10 times before it ends.

turns = 0

while turns < 10
  turns = turns + 1

  # game code

We can also store the number of correct and failed attempts so we can calculate the score of the game. We can do that by incrementing the value of a correct and wrong variables when we check if the answer is correct or not.

To calculate the percentage of correct answers we need to divide the correct answers with the total attempts and multiply the result by 100.

score = 100*...

Output the score after the loop

puts "Rights #{correct}; Wrongs #{wrong}; Score #{score}%"


To get the number of seconds that it takes to run the game we can store the timestamp at the beginning of the game and then subtract it at the end. To do that, we can use

Try outputting in the irb

First create and assign the time to a variable start. Then, subtract - start and output that.

puts "Total time #{duration} seconds"

You can also calculate the average time it take to respond to each problem by dividing the duration with the amount of attempts.

puts "#{duration/turns} seconds per problem"


At the beginning of the game ask for the number of turns. If return is pressed default to 0.

To default value of a variable, only if the variable empty you can use the ||= operant. This is called lazy loading.

# here the number will get assigned to 1 as its value is nil
number = nil
number ||= 1

# The number will not get assigned to 1 because it already has a value
number = 2
number ||= 1

Reading and writing to a file

In Ruby we can use the File object to read and write to files.

To open an existing file we use, mode)

The mode indicates if the file will be opened for reading or writing.

Writing to a file

To write to a file, we must open it using the write mode (w), write the new content and close() it when we are done.

filename = "colors.txt"
file =, "w")
file.puts "red "
file.puts "green"
file.puts "blue"

Alternatively, we can use a block.

filename = "colors.txt", 'w') do |file|

Reading from a file

To read from a file, we first open it and then while there is more content, read each line.'animals.txt', 'r') do |file|
  while line = file.gets
    puts line

Download the animals.txt file used in the example.

Exercise 2: Storing scores

Now that we know how to store information, we can store and load the scores for our numbers game.

Extend the game to ask for the player’s name at the end of the game, and store the name and time each game has taken in the scores.txt file using comma separated values

player1, 12.24
player2, 8.32
player3, 9.12

Then display the five highest scores.

To get the highest scores you can read in each line and then use split(",") to turn it into an array.

while entry = file.gets
  scores << entry.split(",")

You can then use the array sort method.

More practise

Download and try to go through as much of the Ruby Koans as you can.

After you download the repository, you can generate the koans using

rake gen

To regenerate and delete any progress you’ve already make run

rake regen

To try out the koans run rake. It gives you a hint as to what fails and you can move forward by fixing the file and running rake again.

If you are working through this at home you can ask for help in our gitter channel.

Some reading material

Why’s poignant guide to Ruby is a cartoon written about Ruby by a programmer called Why the Lucky Stiff. You can also download the pdf version.

This ends our Ruby basics tutorial. Is there something you don’t understand? Try and go through the provided resources with your coach. If you have any feedback, or can think of ways to improve this tutorial send us an email and let us know.