Flowdock
method

flat_map

Importance_3
Ruby latest stable (v1_9_3_392) - 3 notes - Class: Enumerable
flat_map() public

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.

[[1,2],[3,4]].flat_map {|i| i }   #=> [1, 2, 3, 4]
Show source
Register or log in to add new notes.
November 16, 2011
1 thank

Alternative definition

a.flat_map(&b) works exactly like a.map(&b).flatten!(1).

March 1, 2015
0 thanks

Not exactly like map {}.flatten

To clarify on the last comment, conceptually it’s the same, but #flat_map will perform better because there is no need to create an intermediate Array

May 22, 2015 - (>= v1_8_6_287)
0 thanks

Not exactly like map {}.flatten

To also give dimension, is about 4.5 times faster then map {}.flatten.