📖 Scenario: You work in a small bookstore. You have a list of book titles and their prices. You want to print each book's name and price to share with your team.
🎯 Goal: Build a Ruby program that uses each with for iteration to go through a hash of books and prices, then print each book and its price.
📋 What You'll Learn
Create a hash called books with exact keys and values
Create a variable called total_price and set it to 0
Use for title, price in books.each to iterate over the hash
Add each price to total_price
Print each book title and price inside the loop
After the loop, print the total price
💡 Why This Matters
🌍 Real World
Bookstores and shops often need to list items with prices and calculate totals for sales or inventory.
💼 Career
Knowing how to iterate over collections and sum values is a basic skill for many programming jobs, especially in retail or data processing.
Progress0 / 4 steps
1
Create the books hash
Create a hash called books with these exact entries: 'Ruby Basics' => 25, 'JavaScript Guide' => 30, 'Python 101' => 20
Ruby
Hint
Use curly braces {} to create a hash with keys and values separated by =>.
2
Create a total price variable
Create a variable called total_price and set it to 0
Ruby
Hint
Use = to assign 0 to total_price.
3
Use each for iteration to sum prices and print books
Use for title, price in books.each to iterate over the books hash. Inside the loop, add price to total_price and print the book title and price using puts with the format: "Title: #{title}, Price: $#{price}"
Ruby
Hint
Use for title, price in books.each to loop. Use total_price += price to add prices. Use puts with string interpolation to print.
4
Print the total price
After the loop, print the total price using puts with the format: "Total price: $#{total_price}"
Ruby
Hint
Use puts after the loop to print the total price with string interpolation.
Practice
(1/5)
1. What does the each method do in Ruby when used with an array?
easy
A. It removes duplicate elements from the array.
B. It runs the given block once for every element in the array.
C. It sorts the array in ascending order.
D. It returns the length of the array.
Solution
Step 1: Understand the purpose of each
The each method is used to loop over every element in a collection like an array.
Step 2: Identify what happens inside the block
The block given to each runs once for each element, allowing actions on each item.
Final Answer:
It runs the given block once for every element in the array. -> Option B
Quick Check:
each repeats actions per item [OK]
Hint: Remember: each runs code for every item in the list [OK]
Common Mistakes:
Confusing each with sorting or filtering methods
Thinking each returns a new array
Assuming each counts elements
2. Which of the following is the correct syntax to iterate over an array numbers using each in Ruby?
easy
A. numbers.each do num puts num end
B. each numbers do |num| puts num end
C. numbers.each { |num| puts num }
D. numbers.each(num) { puts num }
Solution
Step 1: Recognize correct each syntax
The each method is called on the array, followed by a block with a parameter inside pipes.
Step 2: Check block format
numbers.each { |num| puts num } uses curly braces with a block parameter |num| and a valid block body puts num.