Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Enumerator::Lazy#filter_map

[U] () { (Elem elem) -> (nil | false | U) } -> ::Array[U]
() -> ::Enumerator[Elem, ::Array[untyped]]
lazy.filter_map { |obj| block } → lazy_enumerator

Like Enumerable#filter_map, but chains operation to be lazy-evaluated.

Example
(1..).lazy.filter_map { |i| i * 2 if i.even? }.first(5)
#=> [4, 8, 12, 16, 20]
See also #filter #find_all #enum_for #flat_map also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0