# Array#find!

**URL:** <https://www.ruby-forum.com/t/array-find/130638>\
**Category:** Ruby\
**Created:** [March 1, 2008, 1:45am UTC](https://www.ruby-forum.com/t/array-find/130638 "2008-03-01T01:45:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [March 1, 2008, 1:45am UTC](https://www.ruby-forum.com/t/array-find/130638/1 "2008-03-01T01:45:57Z")

</div>

How to remove the first occurrence of something?

a=[1,2,3,4]  
a.find!{ |e| a==2 } #=\> 2

But there is no #find!, how to define it? My solutions seem unduly  
complex.

Thanks,  
T.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [March 1, 2008, 2:26am UTC](https://www.ruby-forum.com/t/array-find/130638/2 "2008-03-01T02:26:58Z")

</div>

On 2/29/08, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > T.  
> > end  
> > end

Why not just…  
arr = [1, 2, 2, 2, 3]  
arr.delete\_at(arr.index(2))

arr =\> [1, 2, 2, 3]

Christopher

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [March 1, 2008, 2:30am UTC](https://www.ruby-forum.com/t/array-find/130638/3 "2008-03-01T02:30:29Z")

</div>

7stud – wrote:

> arr.each\_with\_index do |elmt, i|  
> if elmt == 2  
> arr.delete\_at(i)  
> break  
> end  
> end

If you’re going that far, why not just

arr.delete\_at(arr.index(2))

(Of course that’s limited to #== comparisons. Maybe Array#index should  
take a block…)

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [March 1, 2008, 2:15am UTC](https://www.ruby-forum.com/t/array-find/130638/4 "2008-03-01T02:15:33Z")

</div>

Trans wrote:

> How to remove the first occurrence of something?
> 
> a=[1,2,3,4]  
> a.find!{ |e| a==2 } #=\> 2
> 
> But there is no #find!, how to define it? My solutions seem unduly  
> complex.
> 
> Thanks,  
> T.

More complex than the following?

arr = [1, 2, 3, 2, 2, 2]

arr.each\_with\_index do |elmt, i|  
if elmt == 2  
arr.delete\_at(i)  
break  
end  
end

p arr

[1, 3, 2, 2, 2]

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [March 1, 2008, 3:12am UTC](https://www.ruby-forum.com/t/array-find/130638/5 "2008-03-01T03:12:06Z")

</div>

On Sat, 2008-03-01 at 10:15 +0900, 7stud – wrote:

> arr.each\_with\_index do |elmt, i|  
> if elmt == 2  
> arr.delete\_at(i)  
> break  
> end  
> end

This won’t work in 1.9: “RuntimeError: can’t modify array during  
iteration”.

Best,  
Andre

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [March 1, 2008, 3:22am UTC](https://www.ruby-forum.com/t/array-find/130638/6 "2008-03-01T03:22:04Z")

</div>

From: Joel VanderWerf [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# arr.delete\_at(arr.index(2))

# (Of course that’s limited to #== comparisons. Maybe

# Array#index should

# take a block…)

of course, we agree, that is why matz put it in 1.9 😉

irb(main):011:0\> RUBY\_VERSION  
=\> “1.9.0”  
irb(main):012:0\> a  
=\> [“this”, “is”, “a”, “test”]  
irb(main):013:0\> a.index{|e| e=~ /^is/}  
=\> 1  
irb(main):014:0\> a  
=\> [“this”, “is”, “a”, “test”]  
irb(main):015:0\> a.delete\_at(a.index{|e| e=~ /^is/})  
=\> “is”  
irb(main):016:0\> a  
=\> [“this”, “a”, “test”]

and 1.9 is fast 🙂

kind regards -botp
