Sum of Prices Using Reduce/Inject
📖 Scenario: You are managing a small store's inventory. Each product has a price, and you want to find out the total value of all products combined.
🎯 Goal: Build a Ruby program that uses inject to calculate the total price of all products in a dictionary.
📋 What You'll Learn
Create a hash called
products with exact product-price pairsCreate a variable called
total_price to hold the sumUse
inject on products.values to calculate the totalPrint the
total_price value💡 Why This Matters
🌍 Real World
Calculating totals is common in shopping carts, budgets, and reports where you add many numbers together.
💼 Career
Knowing how to use <code>inject</code> helps you write clean and efficient Ruby code for data processing tasks.
Progress0 / 4 steps