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#collect_concat

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

Alias of #flat_map

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 #flat_map #collect #count #chunk_while also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1