📖 Scenario: You work at a small bookstore. You have a list of book titles and their prices. You want to print each book's name with its price to show customers.
🎯 Goal: Build a Ruby program that uses each as the primary iterator to go through a hash of books and prices, then print each book with its price.
📋 What You'll Learn
Create a hash called books with these exact entries: 'Ruby Basics' => 25, 'Rails Guide' => 30, 'JavaScript Essentials' => 20
Create a variable called discount and set it to 5
Use each as the primary iterator with variables title and price to go through books
Inside the loop, subtract discount from price and store it in final_price
Print each book's title and its final_price in the format: "Ruby Basics costs $20"
💡 Why This Matters
🌍 Real World
This project shows how to loop through collections of data, like products and prices, which is common in many business applications.
💼 Career
Knowing how to use <code>each</code> to iterate over hashes is a fundamental skill for Ruby developers working with data collections.
Progress0 / 4 steps
1
Create the books hash
Create a hash called books with these exact entries: 'Ruby Basics' => 25, 'Rails Guide' => 30, 'JavaScript Essentials' => 20
Ruby
Hint
Use curly braces {} to create a hash. Separate keys and values with =>.
2
Add a discount variable
Create a variable called discount and set it to 5
Ruby
Hint
Use a simple assignment to create the variable discount.
3
Use each to iterate over books
Use each as the primary iterator with variables title and price to go through books. Inside the loop, subtract discount from price and store it in final_price
Ruby
Hint
Use books.each do |title, price| ... end to loop. Calculate final_price inside the loop.
4
Print each book's title and final price
Inside the each loop, print each book's title and its final_price in the format: "Ruby Basics costs $20"
Ruby
Hint
Use puts with string interpolation: "#{title} costs $#{final_price}".
Practice
(1/5)
1. What does the each method do in Ruby when used on an array?
easy
A. It returns a new array with modified items.
B. It runs a block of code once for every item in the array.
C. It changes the array by removing items.
D. It sorts the array in ascending order.
Solution
Step 1: Understand the purpose of each
The each method is used to run a block of code for every item in a collection like an array.
Step 2: Check what each does to the array
It does not change or sort the array; it simply processes each item one by one.
Final Answer:
It runs a block of code once for every item in the array. -> Option B
Quick Check:
each runs code on each item [OK]
Hint: Remember: each processes items, does not change array [OK]
Common Mistakes:
Thinking each modifies the array
Confusing each with methods like map
Assuming each returns a new array
2. Which of the following is the correct syntax to use each on an array numbers to print each number?
easy
A. each numbers { |num| puts num }
B. numbers.each do num puts num end
C. numbers.each { |num| puts num }
D. numbers.each |num| puts num
Solution
Step 1: Identify correct block syntax with each
The correct syntax uses each followed by a block with { |variable| ... } or do |variable| ... end.
Step 2: Check each option
numbers.each { |num| puts num } uses numbers.each { |num| puts num } which is correct. Others have syntax errors or wrong order.