📖 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
1
Create the nested array of cookie trays
Create a variable called cookie_boxes and set it to an array containing these arrays exactly: ["Chocolate Chip", "Oatmeal"], ["Peanut Butter", "Sugar"], and ["Snickerdoodle"].
Ruby
Hint
Remember to use square brackets for arrays and double quotes for strings.
2
Prepare to flatten the nested array
Create a variable called all_cookies and set it to nil for now. This will hold the flattened list later.
Ruby
Hint
Just create the variable and assign nil for now.
3
Use flat_map to flatten the nested array
Set the variable all_cookies to the result of calling flat_map on cookie_boxes with a block that returns each inner array as is.
Ruby
Hint
Use flat_map with a block that takes each tray and returns it.
4
Print the flattened list of cookies
Write a puts statement to print the all_cookies array.
Ruby
Hint
Use puts all_cookies to print each cookie type on its own line.
Practice
(1/5)
1. What does the Ruby method flat_map do when applied to an array of arrays?
easy
A. It removes duplicate elements from the array.
B. It only flattens the array without applying any block.
C. It applies a block to each element and flattens the result by one level.
D. It maps elements but keeps the nested structure intact.
Solution
Step 1: Understand flat_map behavior
The flat_map method applies a block to each element of the array.
Step 2: Flatten the mapped results
After mapping, it flattens the resulting array by one level, combining nested arrays into a single array.
Final Answer:
It applies a block to each element and flattens the result by one level. -> Option C
Quick Check:
flat_map = map + flatten(1) [OK]
Hint: Remember: flat_map = map + flatten one level [OK]
Common Mistakes:
Thinking flat_map flattens all nested levels
Confusing flat_map with flatten
Assuming flat_map does not apply a block
2. Which of the following is the correct syntax to use flat_map on an array arr to double each number?
easy
A. arr.flat_map do x * 2 end
B. arr.flat_map { |x| [x * 2] }
C. arr.flat_map { x * 2 }
D. arr.flat_map(x) { x * 2 }
Solution
Step 1: Understand flat_map block return
The block given to flat_map should return an array or enumerable to flatten.
Step 2: Check each option's block return
arr.flat_map { |x| [x * 2] } returns an array [x * 2], which flat_map will flatten by one level, producing a flat array of doubled values.
Final Answer:
arr.flat_map { |x| [x * 2] } -> Option B
Quick Check:
Block must return array for flat_map flattening [OK]
Hint: Return an array inside block for flat_map to flatten [OK]
Common Mistakes:
Returning a single value instead of an array in block
flat_map expects the block to return arrays or enumerables to flatten one level; returning integers causes flattening to fail or unexpected results.
Final Answer:
flat_map block returns integers, not arrays, so flattening fails. -> Option D
Quick Check:
Block must return arrays for flat_map flattening [OK]
Hint: Block must return arrays, not single values, for flat_map [OK]
Common Mistakes:
Returning single values instead of arrays in block
Assuming flat_map works like map without flattening
Misunderstanding flat_map usage on nested arrays
5. Given a nested array of words grouped by their first letter, how can you use flat_map to get a flat array of all words in uppercase?
words = [['apple', 'apricot'], ['banana', 'blueberry'], ['cherry']]
# Fill in the blank:
result = words.flat_map { |group| ________ }
puts result.inspect
hard
A. group.map { |w| w.upcase }
B. group.upcase
C. group.map { w.upcase }
D. group.flat_map { w.upcase }
Solution
Step 1: Map each word to uppercase inside each group
Use group.map { |w| w.upcase } to convert each word in the sub-array to uppercase.
Step 2: Use flat_map to flatten the mapped arrays
flat_map will flatten the arrays of uppercase words into a single flat array.
Final Answer:
group.map { |w| w.upcase } -> Option A
Quick Check:
flat_map + map for nested uppercase flattening [OK]
Hint: Use map inside flat_map block to transform and flatten [OK]
Common Mistakes:
Calling upcase on array instead of strings
Using flat_map inside block unnecessarily
Returning non-array values from block
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.