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

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

Returns a new array containing the truthy results (everything except false or nil) of running the block for every element in enum.

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

Example
(1..10).filter_map { |i| i * 2 if i.even? } #=> [4, 8, 12, 16, 20]
See also #filter #find #entries #find_all also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1