# Enumerable#find return index

**URL:** <https://rubytalk.org/t/enumerable-find-return-index/25774>\
**Category:** ruby-talk\
**Created:** [15 March 2006 04:08 UTC](https://rubytalk.org/t/enumerable-find-return-index/25774 "2006-03-15T04:08:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tod\_McIntyre](https://avatars.discourse-cdn.com/v4/letter/t/aeb1de/32.png) [@Tod\_McIntyre](https://rubytalk.org/u/Tod_McIntyre)\
**Post date:** [15 March 2006 04:08 UTC](https://rubytalk.org/t/enumerable-find-return-index/25774/1 "2006-03-15T04:08:29Z")

</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

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

---

<div class="post-metadata">

**Author:** ![Daniel\_Baird](https://avatars.discourse-cdn.com/v4/letter/d/df788c/32.png) [@Daniel\_Baird](https://rubytalk.org/u/Daniel_Baird)\
**Post date:** [15 March 2006 04:14 UTC](https://rubytalk.org/t/enumerable-find-return-index/25774/2 "2006-03-15T04:14:41Z")

</div>

try  
arr.index(3)

;Daniel

> **···**
>
> On 15/03/06, Tod McIntyre \<todmcintyre@gmail.com\> 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.
> > 
> > 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
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> Daniel Baird  
> [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:** ![Ross\_Bamford4](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@Ross\_Bamford4](https://rubytalk.org/u/Ross_Bamford4)\
**Post date:** [15 March 2006 08:20 UTC](https://rubytalk.org/t/enumerable-find-return-index/25774/3 "2006-03-15T08:20:46Z")

</div>

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):

&nbsp;&nbsp;a = [:a,:b,:c,:d,:e]  
&nbsp;&nbsp;# =\> [:a, :b, :c, :d, :e]

&nbsp;&nbsp;a.index(:a)  
&nbsp;&nbsp;# =\> 0

&nbsp;&nbsp;a.index(:e)  
&nbsp;&nbsp;# =\> 4

> **···**
>
> 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.
> 
> --  
> Ross Bamford - rosco@roscopeco.REMOVE.co.uk

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [15 March 2006 14:23 UTC](https://rubytalk.org/t/enumerable-find-return-index/25774/4 "2006-03-15T14:23:47Z")

</div>

"Tod McIntyre" \<todmcintyre@gmail.com\> wrote in message news:da9c35364b80a5c4e9e603e0db41190a@ruby-forum.com...

> 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

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

&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Tod\_McIntyre](https://avatars.discourse-cdn.com/v4/letter/t/aeb1de/32.png) [@Tod\_McIntyre](https://rubytalk.org/u/Tod_McIntyre)\
**Post date:** [15 March 2006 15:38 UTC](https://rubytalk.org/t/enumerable-find-return-index/25774/5 "2006-03-15T15:38:44Z")

</div>

Robert Klemme wrote:

> "Tod McIntyre" \<todmcintyre@gmail.com\> wrote in message  
> news:da9c35364b80a5c4e9e603e0db41190a@ruby-forum.com...
> 
> > 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
> 
> &nbsp;&nbsp;&nbsp;&nbsp;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.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [15 March 2006 15:53 UTC](https://rubytalk.org/t/enumerable-find-return-index/25774/6 "2006-03-15T15:53:49Z")

</div>

"Tod McIntyre" \<todmcintyre@gmail.com\> wrote in message news:859233703646963b97fe343fb84b6e0f@ruby-forum.com...

> Robert Klemme wrote:
> 
> > "Tod McIntyre" \<todmcintyre@gmail.com\> wrote in message  
> > news:da9c35364b80a5c4e9e603e0db41190a@ruby-forum.com...
> > 
> > > 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
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;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.

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

&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [15 March 2006 16:26 UTC](https://rubytalk.org/t/enumerable-find-return-index/25774/7 "2006-03-15T16:26:06Z")

</div>

Hi --

> 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:

&nbsp;&nbsp;&nbsp;\* given meth and meth!, meth! is the more "dangerous" version  
&nbsp;&nbsp;&nbsp;\* "dangerous" often means "receiver-changing", but definitely  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;does not have to mean that  
&nbsp;&nbsp;&nbsp;\* methods whose names already imply receiver-changing don't  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;have a ! because they don't need one, and also ! methods  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;only come in pairs with a non-! equivalent. (It would be  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hard to imagine what "Replace the contents of this string  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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

> **···**
>
> On Thu, 16 Mar 2006, Robert Klemme wrote:
> 
> --  
> David A. Black (dblack@wobblini.net)  
> Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))
> 
> "Ruby for Rails" chapters now available  
> from Manning Early Access Program! [http://www.manning.com/books/black](http://www.manning.com/books/black)

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [15 March 2006 16:43 UTC](https://rubytalk.org/t/enumerable-find-return-index/25774/8 "2006-03-15T16:43:46Z")

</div>

\<dblack@wobblini.net\> wrote in message news:Pine.LNX.4.64.0603150822080.17960@wobblini.net...

> Hi --
> 
> > 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:
> 
> &nbsp;&nbsp;\* given meth and meth!, meth! is the more "dangerous" version  
> &nbsp;&nbsp;\* "dangerous" often means "receiver-changing", but definitely  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;does not have to mean that

Although it's certainly the most common use of "!".

> &nbsp;&nbsp;\* methods whose names already imply receiver-changing don't  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;have a ! because they don't need one, and also ! methods  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;only come in pairs with a non-! equivalent. (It would be  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hard to imagine what "Replace the contents of this string  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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

&nbsp;&nbsp;&nbsp;&nbsp;robert

> **···**
>
> > On Thu, 16 Mar 2006, Robert Klemme wrote:
