# Remove from an array

**URL:** <https://www.ruby-forum.com/t/remove-from-an-array/65663>\
**Category:** Ruby\
**Created:** [August 20, 2006, 9:49am UTC](https://www.ruby-forum.com/t/remove-from-an-array/65663 "2006-08-20T09:49:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bincedecrabe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/77aa72/32.png) [@bincedecrabe](https://www.ruby-forum.com/u/bincedecrabe)\
**Post date:** [August 20, 2006, 9:49am UTC](https://www.ruby-forum.com/t/remove-from-an-array/65663/1 "2006-08-20T09:49:02Z")

</div>

Hello,  
I have an array, let’s name it items[]  
I want to remove an object in a elegant way  
items.remove(3) does not work  
How should I do ?  
Thanks

---

<div class="post-metadata">

**Author:** ![bincedecrabe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/77aa72/32.png) [@bincedecrabe](https://www.ruby-forum.com/u/bincedecrabe)\
**Post date:** [August 20, 2006, 10:18am UTC](https://www.ruby-forum.com/t/remove-from-an-array/65663/2 "2006-08-20T10:18:27Z")

</div>

Paul L. wrote:

> items.delete\_at(3)
> 
> Just like in the documentation.

Did he want to remove the element at index 3,  
or remove instances of 3?

Hal

---

<div class="post-metadata">

**Author:** ![bincedecrabe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/77aa72/32.png) [@bincedecrabe](https://www.ruby-forum.com/u/bincedecrabe)\
**Post date:** [August 20, 2006, 10:16am UTC](https://www.ruby-forum.com/t/remove-from-an-array/65663/3 "2006-08-20T10:16:27Z")

</div>

Nicolas Benady wrote:

> Hello,  
> I have an array, let’s name it items[]  
> I want to remove an object in a elegant way  
> items.remove(3) does not work  
> How should I do ?  
> Thanks

items.delete\_at(3)

Just like in the documentation.

---

<div class="post-metadata">

**Author:** ![bincedecrabe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/77aa72/32.png) [@bincedecrabe](https://www.ruby-forum.com/u/bincedecrabe)\
**Post date:** [August 20, 2006, 12:06pm UTC](https://www.ruby-forum.com/t/remove-from-an-array/65663/4 "2006-08-20T12:06:33Z")

</div>

Hal F. wrote:

> > > 
> > 
> > items.delete\_at(3)
> > 
> > Just like in the documentation.
> 
> Did he want to remove the element at index 3,  
> or remove instances of 3?

I suppose I should have asked that before replying. 🙂

If your theory is correct, then:

items.delete\_if { |x| x == 3 }

Removes all items matching “3”.

---

<div class="post-metadata">

**Author:** ![bincedecrabe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/77aa72/32.png) [@bincedecrabe](https://www.ruby-forum.com/u/bincedecrabe)\
**Post date:** [August 20, 2006, 12:34pm UTC](https://www.ruby-forum.com/t/remove-from-an-array/65663/5 "2006-08-20T12:34:34Z")

</div>

On 8/20/06, Paul L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > items.remove(3) does not work  
> > > > Did he want to remove the element at index 3,  
> > > > or remove instances of 3?
> 
> I suppose I should have asked that before replying. 🙂
> 
> If your theory is correct, then:
> 
> items.delete\_if { |x| x == 3 }

items.delete 3

> Removes all items matching “3”.

as you said

Cheers  
Robert

–

> Paul L.  
> [http://www.arachnoid.com](http://www.arachnoid.com)

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein

---

<div class="post-metadata">

**Author:** ![bincedecrabe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/77aa72/32.png) [@bincedecrabe](https://www.ruby-forum.com/u/bincedecrabe)\
**Post date:** [August 20, 2006, 2:42pm UTC](https://www.ruby-forum.com/t/remove-from-an-array/65663/6 "2006-08-20T14:42:42Z")

</div>

On Sunday, August 20, 2006, at 7:33 PM, Robert D. wrote:

> > > > Nicolas Benady wrote:
> > 
> > If your theory is correct, then:  
> > Cheers  
> > Robert
> 
> –
> 
> > Paul L.  
> > [http://www.arachnoid.com](http://www.arachnoid.com)

This is slightly off-topic, but…

To remove only the first item matching a value of 3, do this…

items.delete\_at(items.index(3))

\_Kevin  
[www.sciwerks.com](http://www.sciwerks.com)
