Flat_map for nested flattening
📖 Scenario: You work at a bakery that sells boxes of assorted cookies. Each box contains different types of cookies in separate trays. You want to list all cookie types available in all boxes in one flat list.
🎯 Goal: Use Ruby's flat_map method to flatten a nested array of cookie trays into a single array of cookie types.
📋 What You'll Learn
Create a variable
cookie_boxes that holds an array of arrays, each inner array representing cookie types in a box.Create a variable
all_cookies that uses flat_map on cookie_boxes to flatten all cookie types into one array.Print the
all_cookies array.💡 Why This Matters
🌍 Real World
Flattening nested lists is common when you have grouped data but want to work with all items together, like listing all products from multiple categories.
💼 Career
Understanding how to flatten nested data structures helps in data processing, report generation, and simplifying complex data for easier use in software development.
Progress0 / 4 steps