method

delete_if

v2_6_3 - Show latest stable - Class: Array
delete_if()
public

Deletes every element of self for which block evaluates to true.

The array is changed instantly every time the block is called, not after the iteration is over.

See also Array#reject!

If no block is given, an Enumerator is returned instead.

scores = [ 97, 42, 75 ]
scores.delete_if {|score| score < 80 }   #=> [97]

3Notes

NOT Equivalent to Array#reject!

artemave · Nov 9, 201010 thanks

@tadman is wrong. There is a difference and, trust me, it can bite:

1.9.2 > [1,2,3,4].delete_if {|x| x > 10}

=> [1, 2, 3, 4] 1.9.2 > [1,2,3,4].reject! {|x| x > 10} => nil

That is, if reject! hasn't rejected anything, it returns nil.

Equivalent to Array#reject!

tadman · May 13, 20093 thanks

This method is functionally identical to Array#reject!

What artemave said.

tadman · Nov 14, 2012

I'd remove my original note if I could, but I can't see a way how.