Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Array#delete_if

() { (Elem item) -> ::boolish } -> self
() -> ::Enumerator[Elem, self]
delete_if {|element| ... } → self
delete_if → new_numerator

With a block given, calls the block with each element of self; removes the element if the block returns a truthy value; returns self:

Example
a = [:foo, 'bar', 2, 'bat']
a.delete_if {|element| element.to_s.start_with?('b') } # => [:foo, 2]

With no block given, returns a new Enumerator.

Related: see Methods for Deleting.

See also #delete_at #difference #delete #dig also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0