Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Enumerable#reject

() { (E) -> ::boolish } -> ::Array[E]
() -> ::Enumerator[E, ::Array[E]]
reject {|element| ... } → array
reject → enumerator

Returns an array of objects rejected by the block.

With a block given, calls the block with successive elements; returns an array of those elements for which the block returns nil or false:

Example
(0..9).reject {|i| i * 2 if i.even? }                             # => [1, 3, 5, 7, 9]
{foo: 0, bar: 1, baz: 2}.reject {|key, value| key if value.odd? } # => {:foo=>0, :baz=>2}

When no block given, returns an Enumerator.

Related: select.

See also #reduce #reverse_each #partition #select also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0