Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Array#reject
() { (Elem item) -> ::boolish } -> self
() -> ::Enumerator[Elem, self]
reject {|element| ... } → new_array
reject → new_enumerator
array.c
View on GitHub ↗
static VALUE
rb_ary_reject(VALUE ary)
{
VALUE rejected_ary;
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
rejected_ary = rb_ary_new();
ary_reject(ary, rejected_ary);
return rejected_ary;
}
With a block given, returns a new array whose elements are all those from self for which the block returns false or nil:
Example
a = [:foo, 'bar', 2, 'bat'] a1 = a.reject {|element| element.to_s.start_with?('b') } a1 # => [:foo, 2]
With no block given, returns a new Enumerator.
Related: Methods for Fetching.