Expand All


Loops are a way of doing something multiple times. In this loop, we printed each fruit to the screen in order.


  • Use loops to do operations for every element in an array.

  • Use puts to print strings to the screen.

  • Learn the two different syntaxes for creating blocks in Ruby.

Step 1

What do you predict this will do?

Type this in irb:
3.times { puts 'Hip, hip, hooray!'}

The times method will do something as many times as you tell it.

The chunk of code between the brackets is called a block. It will get run each time.

Step 2

We often want to do something to each item in a collection.

Type this in irb:
beatles = [ "John", "Paul", "George", "Ringo"]
beatles.each { |beatle| puts "Hi, my name is #{beatle}"}

The straight up-and-down | is called the 'pipe character'. On a US keyboard, it's typically the shifted version of the \ (backslash) key.

The variable between the pipes is a block variable. It's a way to refer to each item in the collection.

each takes the first element in the array and sends it to the block, which temporarily stores it in the block variable and then runs the code after the pipes. It then goes back and does this again for each of the remaining items in the array.

Type this in irb:
ducks = ['Huey', 'Dewey', 'Louie']
ducks.each { |duck| puts "#{duck} quacks!" }
ducks.each { |zombie| puts "#{zombie} quacks!" }

It doesn't matter what you call your block variable: the previous two statements are exactly equivalent to Ruby. But you should try to name your variables something useful so the code makes sense to you later!

Step 3

Type this in irb:
('a'..'z').each { |letter| puts letter }

In ruby, we have a special construct called a range. A range gives us a shortcut in defining a list of objects that fall within starting and ending point. In the example above, we're defining a range of all the lowercase characters between and including 'a' and 'z'. When used with an each statement, a range will loop through each value within that starting point and ending point.

Type this in irb:
('a'...'z').each { |letter| puts letter }

How is this line different from the previous example? The three dots tells us to omit the ending point.

Step 4

There's a second way to make a block in Ruby. Instead of { }, we surround the code with do** and **end.

Type this in irb:
total = 99
colors = ['red', 'blue', 'green']
colors.each do |color|
  puts "#{total} colors of paint on the wall..."
  puts "Take #{color} down, pass it around..."
  total = total - 1
  puts "#{total} colors of paint on the wall!"

The do ... end syntax is typically used when a block needs to span multiple lines, while the { ... } syntax is for a single line block.


Let's write a program that greets everyone in your group. Use an array to store everyone's name.

Here is a sample of how the program might run.

Oh, hello, Sally Samsonite!
Oh, hello, Johnny Jameson!
Oh, hello, Beth Benitsky!
Oh, hello, Corinne Camelia!

Your program should be written in a way that group members can be removed and added to your array without requiring a change to the rest of your program. Use what you now know about loops so that only one puts statement is in your program.

Let's write a program that adds up a set of numbers in an array. The numbers you can use as a test case are 4, 6, 5, 5, 10

Here is a sample of how the program might run.

The sum of all of the numbers is 30.

Your program should be written in a way that doesn't require massive changes when the array of numbers is modified.


As you build complex programs, you'll want to do something to many pieces of data without typing it all out. Loops help solve this problem.

Next Step: