Skip to content

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

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.

See also #reject! #rassoc #push #repeated_combination also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0