# Array#delete\_if! - a method to delete and return all array i

**URL:** <https://www.ruby-forum.com/t/array-delete-if-a-method-to-delete-and-return-all-array-i/61668>\
**Category:** Ruby\
**Created:** [May 26, 2006, 12:05am UTC](https://www.ruby-forum.com/t/array-delete-if-a-method-to-delete-and-return-all-array-i/61668 "2006-05-26T00:05:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@alder](https://www.ruby-forum.com/u/alder)\
**Post date:** [May 26, 2006, 12:05am UTC](https://www.ruby-forum.com/t/array-delete-if-a-method-to-delete-and-return-all-array-i/61668/1 "2006-05-26T00:05:43Z")

</div>

Here’s a method to delete and return all items in an Array for which  
the block evaluates to true:

class Array  
def delete\_if!  
to\_delete = []  
to\_keep = []  
each do |item|  
(yield item) ? to\_delete \<\< item : to\_keep \<\< item  
end  
replace(to\_keep)  
return to\_delete  
end  
end

Demonstration:  
irb(main):002:0\> arr = (1…12).to\_a  
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]  
irb(main):003:0\> arr.delete\_if! {|i| (i % 3) == 0}  
[3, 6, 9, 12]  
irb(main):004:0\> arr  
[1, 2, 4, 5, 7, 8, 10, 11]

Would people suggest better implementations? The obvious problem here  
is that we’re duplicating arr (to\_delete and to\_keep contatin together  
a cloned arr). I came up with an index based solution to avoid that,  
but frankly it was quite ugly.

---

<div class="post-metadata">

**Author:** ![alder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@alder](https://www.ruby-forum.com/u/alder)\
**Post date:** [May 26, 2006, 12:17am UTC](https://www.ruby-forum.com/t/array-delete-if-a-method-to-delete-and-return-all-array-i/61668/2 "2006-05-26T00:17:54Z")

</div>

On May 25, 2006, at 3:03 PM, Alder G. wrote:

> Here’s a method to delete and return all items in an Array for which  
> the block evaluates to true:

[…]

> Would people suggest better implementations? The obvious problem here  
> is that we’re duplicating arr (to\_delete and to\_keep contatin together  
> a cloned arr). I came up with an index based solution to avoid that,  
> but frankly it was quite ugly.

## $ ri Array#reject! ---------------------------------------------------------- Array#reject! array.reject! {|item| block } → array or nil

```
  Equivalent to +Array#delete_if+, deleting elements from _self_ for
  which the block evaluates to true, but returns +nil+ if no changes
  were made. Also see +Enumerable#reject+.

```

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![alder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@alder](https://www.ruby-forum.com/u/alder)\
**Post date:** [May 26, 2006, 12:21am UTC](https://www.ruby-forum.com/t/array-delete-if-a-method-to-delete-and-return-all-array-i/61668/3 "2006-05-26T00:21:03Z")

</div>

On May 25, 2006, at 3:15 PM, Eric H. wrote:

> > together  
> > a cloned arr). I came up with an index based solution to avoid that,  
> > but frankly it was quite ugly.
> 
> $ ri Array#reject!

Oops, I meant:

## $ ri partition --------------------------------------------------- Enumerable#partition enum.partition {| obj | block } =\> [true\_array, false\_array]

```
  Returns two arrays, the first containing the elements of _enum_

```

for  
which the block evaluates to true, the second containing the rest.

```
     (1..6).partition {|i| (i&1).zero?} #=> [[2, 4, 6], [1, 3, 5]]

```

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![alder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@alder](https://www.ruby-forum.com/u/alder)\
**Post date:** [May 26, 2006, 1:53am UTC](https://www.ruby-forum.com/t/array-delete-if-a-method-to-delete-and-return-all-array-i/61668/4 "2006-05-26T01:53:20Z")

</div>

> Thanks. I didn’t know about #partition.
> 
> Still, if anyone can suggest an improved pure-ruby solution, I’d be  
> interested for educational reasons.

class Array  
def delete\_if!  
to\_delete = []  
delete\_if do |item|  
to\_delete \<\< item if yield item  
end  
to\_delete  
end  
end

irb(main):010:0\> arr = [1,2,3,4,5,6]  
=\> [1, 2, 3, 4, 5, 6]  
irb(main):011:0\> arr.delete\_if! {|x| x \> 3}  
=\> [4, 5, 6]  
irb(main):012:0\> arr  
=\> [1, 2, 3]

---

<div class="post-metadata">

**Author:** ![alder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@alder](https://www.ruby-forum.com/u/alder)\
**Post date:** [May 26, 2006, 1:32am UTC](https://www.ruby-forum.com/t/array-delete-if-a-method-to-delete-and-return-all-array-i/61668/5 "2006-05-26T01:32:10Z")

</div>

On 5/26/06, Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On May 25, 2006, at 3:15 PM, Eric H. wrote:  
> …  
> $ ri partition
> 
> –  
> Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
> This implementation is HODEL-HASH-9600 compliant
> 
> [http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

Thanks. I didn’t know about #partition.

Still, if anyone can suggest an improved pure-ruby solution, I’d be  
interested for educational reasons.

---

<div class="post-metadata">

**Author:** ![alder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@alder](https://www.ruby-forum.com/u/alder)\
**Post date:** [May 26, 2006, 2:05am UTC](https://www.ruby-forum.com/t/array-delete-if-a-method-to-delete-and-return-all-array-i/61668/6 "2006-05-26T02:05:48Z")

</div>

On 5/26/06, Daniel S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> to_delete << item if yield item
> 
> ```
> 
> =\> [1, 2, 3]

Yup, good one. Thanks.
