Iteration

Back

Learning Goals

  • Explain the concept of iterating over an Array
  • Implement syntax for each for a variety of situations

Vocabulary

  • code block
  • iterate
  • method

each

This video explains the each method with a similar example to what is below

each is a method in Ruby that can be used to iterate over an Array. This means, it can take action on each element of an Array, even when the code for that action is only written once. Look at the following code snippet:

nums = [1, 2, 3, 4]

nums.each do |num|
  puts "The current number is #{num}."
end

# Output:
# The current number is 1
# The current number is 2
# The current number is 3
# The current number is 4

Let’s break down each part of the code:

  • nums - we first must tell Ruby which Array we’d like to iterate over
  • .each - we call the each method on the Array. Ruby now expects the following:
    • do - this is a Ruby keyword that must follow each; it tells the program to start taking action
    • |num| - inside the pipe characters (|, which can be found just under the delete key on your keyboard), the developer decides on a name for a single element in the Array. Usually, this is the singular form of the Arrays variable name
    • 1 or more lines of code, indented. This code will run for each of the elements in the Array. If there are 100 elements, this line will run 100 times. If there is 1 element, this line will run 1 time
    • end - this tells the program we are done with the each block, and is required.
  • Anything written inside of the do and end is referred to as the code block

We can also read through annotations of the same code snippet to understand what each part does:

# declare a variable nums that stores an Array of 4 Integers
nums = [1, 2, 3, 4]

# Call the each method on nums Array
# Use "num" as variable name for each element as it's being iterated over
nums.each do |num|
  # puts an interpolated sentence using the current value of num
  puts "The current number is #{num}."
# end the each block
end

# The each block will run 4 times because there are 4 elements in the Array
# The first time it runs, the Integer 1 is the value of the num variable
# The second time it runs, the Integer 2 is the value of the num variable
# The third time it runs, the Integer 3 is the value of the num variable
# The fourth time it runs, the Integer 4 is the value of the num variable
# Once it's run on every element in the Array, it reads the end keyword, and is finished

Predict What Will Happen

Read each of the following code snippets and predict what the output will be. Then, run the code in irb (or in your preferred environment) to confirm your prediction.

balances = [100, 49, 98, 73, 56]

balances.each do |balance|
  puts balance + 10
end

# How many times did the code in the each block run? Why?
names = ["Cindy", "Oscar", "Rigo", "Joe", "Stephanie", "Tiff"]

names.each do |name|
  puts name.length
end

# How many times did the code in the each block run? Why?
# What was the value of "name" the first time the code ran?

Practice

Use the each method to solve each problem. You can do this work in the place that works best for you - irb, a Ruby file you create and open in VS Code, or somewhere else.

Print out a greeting to each friend:

friends = ["Joe", "Jeff", "Alex", "Justina"]


Find the square of each number:

numbers = [2, 4, 6, 8, 10, 12]


Print out each String in reverse (not sure how to do that? Google!):

words = ["sunny", "beach", "waves", "relax"]


Challenge: Print out numbers that are greater than 10:

numbers = [10, 11, 7, 19, 4, 52, 89, 9, 12, 10]


Challenge: Print out only names that begin with "P":

names = ["Pilar", "Peach", "Pamela", "Tan", "Amanda", "Phil"]


Addtional Practice

This practice work is encouraged, but not required.

Follow the directions in the README of this GitHub repository.





Back