Skip to content
instance method

Enumerator::Lazy#flat_map

[U] () { (Elem) -> (::Array[U] | U) } -> ::Array[U]
() -> ::Enumerator[Elem, ::Array[untyped]]
lazy.flat_map { |obj| block } → a_lazy_enumerator

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

Example
["foo", "bar"].lazy.flat_map {|i| i.each_char.lazy}.force
#=> ["f", "o", "o", "b", "a", "r"]

A value x returned by block is decomposed if either of the following conditions is true:

  • x responds to both each and force, which means that x is a lazy enumerator.

  • x is an array or responds to to_ary.

Otherwise, x is contained as-is in the return value.

Example
[{a:1}, {b:2}].lazy.flat_map {|i| i}.force
#=> [{:a=>1}, {:b=>2}]
See also #find_all #force #filter_map #grep also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0