Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Enumerable#flat_map

enum.flat_map { |obj| block } → array
enum.flat_map → an_enumerator

Returns a new array with the concatenated results of running block once for every element in enum.

If no block is given, an enumerator is returned instead.

Example
[1, 2, 3, 4].flat_map { |e| [e, -e] } #=> [1, -1, 2, -2, 3, -3, 4, -4]
[[1, 2], [3, 4]].flat_map { |e| e + [100] } #=> [1, 2, 100, 3, 4, 100]
See also #first #grep #find_index #grep_v also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1