# Enumerable#find return index

**URL:** <https://www.ruby-forum.com/t/enumerable-find-return-index/54908>\
**Category:** Ruby\
**Created:** [March 15, 2006, 5:08am UTC](https://www.ruby-forum.com/t/enumerable-find-return-index/54908 "2006-03-15T05:08:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tizzod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4da419/32.png) [@tizzod](https://www.ruby-forum.com/u/tizzod)\
**Post date:** [March 15, 2006, 5:08am UTC](https://www.ruby-forum.com/t/enumerable-find-return-index/54908/1 "2006-03-15T05:08:13Z")

</div>

This is quite embarassing because I Don’t consider myself a poor  
programmer, but I just started looking at ruby the other day.

Blocks seem to be a good way to iterate through something to search for  
an entry for instance. It’s easy to return the object that meets  
certain criteria using blocks but I’m finding it hard to return the  
index of that particular object in, say, an array.

as an example

arr=[4,1,3,7]  
arr.find { |f| f==someval }  
will return the val within arr that matches someval, which doesn’t  
really help me because I already have someval

How do I actually return the index in arr to the first match of someval.

so if someval == 3, my block would return 2, for the above example

---

<div class="post-metadata">

**Author:** ![tizzod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4da419/32.png) [@tizzod](https://www.ruby-forum.com/u/tizzod)\
**Post date:** [March 15, 2006, 5:15am UTC](https://www.ruby-forum.com/t/enumerable-find-return-index/54908/2 "2006-03-15T05:15:58Z")

</div>

try  
arr.index(3)

;Daniel

On 15/03/06, Tod McIntyre [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

–  
Daniel B.  
[http://danielbaird.com](http://danielbaird.com) (TiddlyW;nks! :: Whiteboard Koala :: Blog ::  
Things  
That Suck)  
[[My webhost uptime is ~ 92%… if no answer pls call again later!]]

---

<div class="post-metadata">

**Author:** ![tizzod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4da419/32.png) [@tizzod](https://www.ruby-forum.com/u/tizzod)\
**Post date:** [March 15, 2006, 9:23am UTC](https://www.ruby-forum.com/t/enumerable-find-return-index/54908/3 "2006-03-15T09:23:37Z")

</div>

On Wed, 2006-03-15 at 13:08 +0900, Tod McIntyre wrote:

> This is quite embarassing because I Don’t consider myself a poor  
> programmer, but I just started looking at ruby the other day.
> 
> Blocks seem to be a good way to iterate through something to search for  
> an entry for instance. It’s easy to return the object that meets  
> certain criteria using blocks but I’m finding it hard to return the  
> index of that particular object in, say, an array.

Check out this thread:

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/178495](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/178495)

(The upshot is that in CVS, 1.9 now allows a block to #index to specify  
what is to be found).

In the meantime, the current #index should do the job for most cases  
(e.g. the example you showed):

```
a = [:a,:b,:c,:d,:e]
# => [:a, :b, :c, :d, :e]

a.index(:a)
# => 0

a.index(:e)
# => 4

```

---

<div class="post-metadata">

**Author:** ![tizzod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4da419/32.png) [@tizzod](https://www.ruby-forum.com/u/tizzod)\
**Post date:** [March 15, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/enumerable-find-return-index/54908/4 "2006-03-15T16:38:28Z")

</div>

Robert K. wrote:

> “Tod McIntyre” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
> news:removed\_email\_address@domain.invalid…
> 
> > arr=[4,1,3,7]  
> > arr.find { |f| f==someval }  
> > will return the val within arr that matches someval, which doesn’t  
> > really help me because I already have someval
> 
> Yeah, but this is just a special case. With a block you can employ  
> arbitrary selection criteria - not just ==.
> 
> > How do I actually return the index in arr to the first match of someval.
> > 
> > so if someval == 3, my block would return 2, for the above example
> 
> As Ross and Daniel have pointed out already, use #index.
> 
> Kind regards
> 
> ```
> robert
> 
> ```

Once again I knew this would be a simple answer!! I thank you for your  
quick responses. Obviously I should be reading the api more thoroughly  
before posting.

---

<div class="post-metadata">

**Author:** ![tizzod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4da419/32.png) [@tizzod](https://www.ruby-forum.com/u/tizzod)\
**Post date:** [March 15, 2006, 4:54pm UTC](https://www.ruby-forum.com/t/enumerable-find-return-index/54908/5 "2006-03-15T16:54:04Z")

</div>

“Tod McIntyre” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

> > 
> 
> Once again I knew this would be a simple answer!! I thank you for your  
> quick responses. Obviously I should be reading the api more thoroughly  
> before posting.

Ah, don’t bother. The API of the standard lib has it’s humps and bumps  
(for example not fully consistent usage of ! for destructive methods  
etc.)  
so it’s normal to fall into one or the other pit initially.

Kind regards

```
robert

```

---

<div class="post-metadata">

**Author:** ![tizzod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4da419/32.png) [@tizzod](https://www.ruby-forum.com/u/tizzod)\
**Post date:** [March 15, 2006, 3:26pm UTC](https://www.ruby-forum.com/t/enumerable-find-return-index/54908/6 "2006-03-15T15:26:38Z")

</div>

“Tod McIntyre” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

> arr=[4,1,3,7]  
> arr.find { |f| f==someval }  
> will return the val within arr that matches someval, which doesn’t  
> really help me because I already have someval

Yeah, but this is just a special case. With a block you can employ  
arbitrary selection criteria - not just ==.

> How do I actually return the index in arr to the first match of someval.
> 
> so if someval == 3, my block would return 2, for the above example

As Ross and Daniel have pointed out already, use #index.

Kind regards

```
robert

```

---

<div class="post-metadata">

**Author:** ![tizzod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4da419/32.png) [@tizzod](https://www.ruby-forum.com/u/tizzod)\
**Post date:** [March 15, 2006, 5:26pm UTC](https://www.ruby-forum.com/t/enumerable-find-return-index/54908/7 "2006-03-15T17:26:12Z")

</div>

Hi –

On Thu, 16 Mar 2006, Robert K. wrote:

> Ah, don’t bother. The API of the standard lib has it’s humps and bumps (for  
> example not fully consistent usage of ! for destructive methods etc.) so it’s  
> normal to fall into one or the other pit initially.

I have to leap to the defense of ! 🙂 It really is consistent:

- given meth and meth!, meth! is the more “dangerous” version
- “dangerous” often means “receiver-changing”, but definitely  
does not have to mean that
- methods whose names already imply receiver-changing don’t  
have a ! because they don’t need one, and also ! methods  
only come in pairs with a non-! equivalent. (It would be  
hard to imagine what “Replace the contents of this string  
object, but without changing the object” would mean…)

“Implying receiver-changing” is of course in the eyes of Matz 🙂 But  
while there are judgements, I don’t think there’s any inconsistency.

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” chapters now available  
from Manning Early Access Program! [Ruby for Rails](http://www.manning.com/books/black)

---

<div class="post-metadata">

**Author:** ![tizzod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4da419/32.png) [@tizzod](https://www.ruby-forum.com/u/tizzod)\
**Post date:** [March 15, 2006, 5:46pm UTC](https://www.ruby-forum.com/t/enumerable-find-return-index/54908/8 "2006-03-15T17:46:33Z")

</div>

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

> - given meth and meth!, meth! is the more “dangerous” version
> - “dangerous” often means “receiver-changing”, but definitely  
> does not have to mean that

Although it’s certainly the most common use of “!”.

> - methods whose names already imply receiver-changing don’t  
> have a ! because they don’t need one, and also ! methods  
> only come in pairs with a non-! equivalent. (It would be  
> hard to imagine what “Replace the contents of this string  
> object, but without changing the object” would mean…)
> 
> “Implying receiver-changing” is of course in the eyes of Matz 🙂 But  
> while there are judgements, I don’t think there’s any inconsistency.

Um, yes. I should print this out and place it in front of my monitor.  
Somehow I keep forgetting this definition - must be some old newsgroup  
thread having anchored deep in my subconscious.

Thanks for putting that straight!

Kind regards

```
robert

```
