Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Array#reject

array.reject {|element| ... } → new_array
array.reject → new_enumerator

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]

Returns a new Enumerator if no block given:

Example
a = [:foo, 'bar', 2]
a.reject # => #<Enumerator: [:foo, "bar", 2]:reject>
See also #reject! #rassoc #push #repeated_combination also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0