Using Each as the Primary Iterator in Ruby
📖 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' => 20Create a variable called
discount and set it to 5Use
each as the primary iterator with variables title and price to go through booksInside the loop, subtract
discount from price and store it in final_pricePrint 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