0
0
Rubyprogramming~10 mins

Flat_map for nested flattening in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to flatten the nested arrays using flat_map.

Ruby
nested = [[1, 2], [3, 4], [5, 6]]
flat = nested.[1] { |arr| arr }
puts flat.inspect
Drag options to blanks, or click blank then click option'
Aselect
Bflat_map
Ceach
Dmap
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'map' only returns nested arrays without flattening.
Using 'each' does not return a new array.
Using 'select' filters elements but does not flatten.
2fill in blank
medium

Complete the code to flatten and double each number in the nested arrays.

Ruby
nested = [[1, 2], [3, 4], [5, 6]]
result = nested.[1] { |arr| arr.map { |n| n * 2 } }
puts result.inspect
Drag options to blanks, or click blank then click option'
Aflat_map
Bmap
Ceach
Dselect
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'map' results in nested arrays after doubling.
Using 'each' does not return a new array.
Using 'select' filters elements but does not transform or flatten.
3fill in blank
hard

Fix the error in the code to flatten nested arrays of strings into a single array of characters.

Ruby
words = [["hi", "there"], ["ruby", "code"]]
chars = words.[1] { |word_arr| word_arr.map(&:chars) }
puts chars.flatten.inspect
Drag options to blanks, or click blank then click option'
Aflat_map
Bmap
Ceach
Dselect
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'map' returns nested arrays requiring extra flattening.
Using 'each' does not return a new array.
Using 'select' filters but does not flatten.
4fill in blank
hard

Fill both blanks to flatten and convert nested arrays of numbers to strings.

Ruby
nested = [[1, 2], [3, 4], [5, 6]]
result = nested.[1] { |arr| arr.[2](&:to_s) }
puts result.inspect
Drag options to blanks, or click blank then click option'
Aflat_map
Bmap
Ceach
Dselect
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'map' instead of 'flat_map' on the outer array keeps nesting.
Using 'each' instead of 'map' inside does not transform elements.
5fill in blank
hard

Fill all three blanks to flatten nested arrays and select only even numbers as strings.

Ruby
nested = [[1, 2], [3, 4], [5, 6]]
result = nested.[1] { |arr| arr.[2] { |n| n.[3] }}
puts result.inspect
Drag options to blanks, or click blank then click option'
Aflat_map
Bselect
Ceven?
Dmap
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'map' instead of 'flat_map' keeps nested arrays.
Using 'map' instead of 'select' filters incorrectly.
Using wrong condition instead of 'even?'.