instance method
Array#keep_if
() { (Elem item) -> ::boolish } -> self
() -> ::Enumerator[Elem, self]
keep_if {|element| ... } → self
keep_if → new_enumerator
array.c
View on GitHub ↗
static VALUE
rb_ary_keep_if(VALUE ary)
{
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
rb_ary_select_bang(ary);
return ary;
}
With a block given, calls the block with each element of self; removes the element from self if the block does not return a truthy value:
Example
a = [:foo, 'bar', 2, :bam] a.keep_if {|element| element.to_s.start_with?('b') } # => ["bar", :bam]
With no block given, returns a new Enumerator.
Related: see Methods for Deleting.