Block syntax (do..end and curly braces)
📖 Scenario: You are helping a small bakery keep track of daily sales. Each sale has a product name and the quantity sold. You want to use Ruby blocks to process this sales data.
🎯 Goal: Learn how to use Ruby blocks with both do..end and curly braces {} syntax to iterate over sales data and calculate total items sold.
📋 What You'll Learn
Create a hash called
sales with exact product names and quantitiesCreate a variable
total_items to hold the sum of all quantitiesUse a
do..end block with each to add quantities to total_itemsUse a curly braces
{} block with each to print each product and quantityPrint the total number of items sold
💡 Why This Matters
🌍 Real World
Blocks let you process collections like sales data easily, which is common in business apps.
💼 Career
Understanding block syntax is essential for Ruby developers working on data processing, web apps, and automation.
Progress0 / 4 steps