Episode 17b - Modifying Values Outside of the Loop

You can use the "each" method to modify values outside of the each loop. For example, if you had an array of words and wanted to combine them into a single sentence separated by spaces, you can use this code:

words = ["This", "is", "a", "sentence."]
sentence = ""
words.each do |word|
  sentence += word + " "
puts sentence

The result should be:

This is a sentence. 


  1. Start with the array
    words = ["a", "man", "a", "plan", "a", "canal", "panama"]
    Use the "each" method to combine all the words into a single word in all capital letters. The result should be:
  2. Start with the array
    numbers = [3, 7, 2, 10]
    Use the "each" method to compute the product of all the numbers. The result should be:
  3. Start with the array
    titles = ["The Boy Who Lived", "The Vanishing Glass", "The Letters from No One"]
    Use the "each" method to print out each chapter along with its chapter number. The result should be:
    Chapter 1. The Boy Who Lived
    Chapter 2. The Vanishing Glass
    Chapter 3. The Letters from No One