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
enum.c
View on GitHub ↗
static VALUE
enum_filter_map(VALUE obj)
{
VALUE ary;
RETURN_SIZED_ENUMERATOR(obj, 0, 0, enum_size);
ary = rb_ary_new();
rb_block_call(obj, id_each, 0, 0, filter_map_i, ary);
return ary;
}
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]