# Array position

**URL:** <https://www.ruby-forum.com/t/array-position/83305>\
**Category:** Rails\
**Created:** [January 25, 2007, 12:04am UTC](https://www.ruby-forum.com/t/array-position/83305 "2007-01-25T00:04:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![canner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f9ae1b/32.png) [@canner](https://www.ruby-forum.com/u/canner)\
**Post date:** [January 25, 2007, 12:04am UTC](https://www.ruby-forum.com/t/array-position/83305/1 "2007-01-25T00:04:55Z")

</div>

Arrays have the .include? function which tells you if an array has an  
element.

is there a .position? function to get the location of that element? I  
can loop through no problem, but was just wondering if there was  
anything built in.

---

<div class="post-metadata">

**Author:** ![canner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f9ae1b/32.png) [@canner](https://www.ruby-forum.com/u/canner)\
**Post date:** [January 25, 2007, 12:26am UTC](https://www.ruby-forum.com/t/array-position/83305/2 "2007-01-25T00:26:05Z")

</div>

Hi Alex,

Alex T. wrote:

> Arrays have the .include? function which tells  
> you if an array has an element.
> 
> is there a .position? function to get the location  
> of that element?

index (and it’s a method (aka action), not a function)

From the Ruby Library Reference section of Pickaxe (aka “Programming  
Ruby”),  
p434, in the Array section, right under the include? method: “Returns  
the  
index of the first object in arr that is == to obj. Returns nil if no  
match is found.”

a=[“a”,“b”,“c”]  
a.index(“b”) --\> 1

Pick up the Pickaxe. It’s indispensible if you’re going to work in Ruby  
on  
Rails.

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![canner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f9ae1b/32.png) [@canner](https://www.ruby-forum.com/u/canner)\
**Post date:** [January 25, 2007, 3:29pm UTC](https://www.ruby-forum.com/t/array-position/83305/3 "2007-01-25T15:29:12Z")

</div>

Thanks for the clear explanation Bill. I was looking online for info,  
but the sites I checked never mentioned that function.

As soon as I get through my current book, Pickaxe will be next on the  
list.

Alex

---

<div class="post-metadata">

**Author:** ![canner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f9ae1b/32.png) [@canner](https://www.ruby-forum.com/u/canner)\
**Post date:** [January 25, 2007, 4:41pm UTC](https://www.ruby-forum.com/t/array-position/83305/4 "2007-01-25T16:41:40Z")

</div>

Hi Alex,

> Thanks for the clear explanation Bill.

You’re welcome. Glad it was helpful.

> I was looking online for info, but the sites I checked  
> never mentioned that function.

As RoR’s ease-of-use attracts more who are new to web application  
development), the sophistication of the current Ruby and Rails online  
documentation increasingly comes up as ‘problem area’. My take on it  
is:  
It is what it is, not what it will become. Timing is a personal choice.  
Having said that, there are, IME, four key resources, the use of which  
will  
enable your success at this point: Google, The Books, this list, and  
tenacity.

> As soon as I get through my current book, Pickaxe will  
> be next on the list.

Pickaxe isn’t like that. In addition to the Ruby tutorial, it’s the  
definitive English-language Ruby language reference. I’ve learned tons  
just  
cruising through the methods in the Class modules. And I think that’s  
really helped my intuition about how to approach new stuff. I recommend  
ordering it today.

Best regards,  
Bill
