# Delete in Array (if exist) and count

**URL:** <https://www.ruby-forum.com/t/delete-in-array-if-exist-and-count/105832>\
**Category:** Ruby\
**Created:** [June 3, 2007, 4:21pm UTC](https://www.ruby-forum.com/t/delete-in-array-if-exist-and-count/105832 "2007-06-03T16:21:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [June 3, 2007, 4:21pm UTC](https://www.ruby-forum.com/t/delete-in-array-if-exist-and-count/105832/1 "2007-06-03T16:21:11Z")

</div>

I am trying to delete an element from an array if it exists and count  
the number of item afterwards

I wrote

anArray = [1,2,3,4,5,6,7,8,9]  
anArray.size  
=\> 9  
anArray.map {|e| e if e != 4}.compact  
=\> [1, 2, 3, 5, 6, 7, 8, 9]  
anArray.map {|e| e if e != 4}.compact.size  
=\> 8

is ther a better way to do it… it it’s enough ? (learning always how  
to write better code…)

thanks

joss

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [June 3, 2007, 4:36pm UTC](https://www.ruby-forum.com/t/delete-in-array-if-exist-and-count/105832/2 "2007-06-03T16:36:13Z")

</div>

Josselin wrote:

> I am trying to delete an element from an array if it exists and count  
> the number of item afterwards

How about using Array#delete\_if?

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [June 3, 2007, 4:58pm UTC](https://www.ruby-forum.com/t/delete-in-array-if-exist-and-count/105832/3 "2007-06-03T16:58:15Z")

</div>

On 6/3/07, Josselin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> anArray.map {|e| e if e != 4}.compact.size  
> =\> 8
> 
> is ther a better way to do it… it it’s enough ? (learning always how  
> to write better code…)

Array#delete works also with no need for a block (i.e. you’re only  
checking for equality)

anArray = [1,2,3,4,5,6]  
p anArray.delete(4)  
p anArray.size

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [June 3, 2007, 5:52pm UTC](https://www.ruby-forum.com/t/delete-in-array-if-exist-and-count/105832/4 "2007-06-03T17:52:22Z")

</div>

On 2007-06-03 16:57:43 +0200, “Todd B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

> > =\> [1, 2, 3, 5, 6, 7, 8, 9]  
> > anArray = [1,2,3,4,5,6]  
> > p anArray.delete(4)  
> > p anArray.size

I checked  
anArray.delete(0)  
=\> nil  
then  
anArray.delete(0).size  
NoMethodError: undefined method `size’ for nil:NilClass

as I tried to write it in one line…

thanks

joss

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [June 3, 2007, 5:54pm UTC](https://www.ruby-forum.com/t/delete-in-array-if-exist-and-count/105832/5 "2007-06-03T17:54:22Z")

</div>

On 2007-06-03 16:35:16 +0200, Tim H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

> Josselin wrote:
> 
> > I am trying to delete an element from an array if it exists and count  
> > the number of item afterwards
> 
> How about using Array#delete\_if?

that’s exact§ly what I want… I read the API doc, I look into the  
delete… but why I stop before the delete\_if ???  
thanks a lot …

Joss

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [June 3, 2007, 8:59pm UTC](https://www.ruby-forum.com/t/delete-in-array-if-exist-and-count/105832/6 "2007-06-03T20:59:50Z")

</div>

On 6/3/07, Josselin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > =\> 9  
> > > checking for equality)  
> > > NoMethodError: undefined method `size’ for nil:NilClass
> 
> as I tried to write it in one line…
> 
> thanks
> 
> joss

That’s because Array#delete returns the deletion, not the Array  
itself, so, if it has to be on one line, then:

(anArray.delete(4); a).size

or

anArray.delete\_if {|i| i == 4}.size

or I prefer

anArray.reject! {|i| i == 4}.size

#delete, #delete\_if, and #reject! all change the array while #reject  
does not
