Introduction
Flat_map helps you turn a list of lists into a single list by combining all inner lists into one. It makes nested data easier to work with.
array.flat_map { |element| block }[[1, 2], [3, 4]].flat_map { |a| a }
[1, 2, 3].flat_map { |n| [n, n * 2] }
nested = [["apple", "banana"], ["carrot", "date"], ["egg"]] flat = nested.flat_map { |group| group } puts flat.inspect
flat_map do when applied to an array of arrays?flat_map method applies a block to each element of the array.flat_map on an array arr to double each number?flat_map should return an array or enumerable to flatten.[x * 2], which flat_map will flatten by one level, producing a flat array of doubled values.arr = [[1, 2], [3, 4]]
result = arr.flat_map { |sub| sub.map { |x| x * 2 } }
puts result.inspectsub.map { |x| x * 2 } creates a new array with doubled values: [2,4] and [6,8].arr = [1, 2, 3, 4]
result = arr.flat_map { |x| x * 2 }
puts result.inspect{ |x| x * 2 } returns integers, not arrays or enumerables.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.inspectgroup.map { |w| w.upcase } to convert each word in the sub-array to uppercase.Pass the test and this pattern moves unknown