# How to know the index value on the each method

**URL:** <https://www.ruby-forum.com/t/how-to-know-the-index-value-on-the-each-method/159241>\
**Category:** Ruby\
**Created:** [February 2, 2009, 6:13pm UTC](https://www.ruby-forum.com/t/how-to-know-the-index-value-on-the-each-method/159241 "2009-02-02T18:13:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [February 2, 2009, 6:13pm UTC](https://www.ruby-forum.com/t/how-to-know-the-index-value-on-the-each-method/159241/1 "2009-02-02T18:13:47Z")

</div>

Hello,

I was wondering if there is a way to access the current index value when  
using the each method to iterate over an array. For example,

@pancakes.each do |pancake|

puts pancake

# How can I ask which is the current index value of pancake. I wish to

know in what position is the pancake object on the array without using a  
local integer variable to count up.

end

Thanks,

ElÃ­as

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [February 2, 2009, 6:18pm UTC](https://www.ruby-forum.com/t/how-to-know-the-index-value-on-the-each-method/159241/2 "2009-02-02T18:18:20Z")

</div>

Alle Monday 02 February 2009, Elias O. ha scritto:

> local integer variable to count up.
> 
> end
> 
> Thanks,
> 
> ElÃ­as

You can use Array#each\_with\_index, instead of Array#each. Or you can use  
Array#index to obtain the index of the current object, but it only works  
if  
the array doesn’t contain duplicates: if there are duplicates (according  
to  
==), Array#index always returns the index of the first item.

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [February 2, 2009, 6:21pm UTC](https://www.ruby-forum.com/t/how-to-know-the-index-value-on-the-each-method/159241/3 "2009-02-02T18:21:51Z")

</div>

- Elias O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) [2009-02-03 02:13:32 +0900]:

> Hello,
> 
> I was wondering if there is a way to access the current index value when  
> using the each method to iterate over an array. For example,
> 
> @pancakes.each do |pancake|

@pancakes.each\_with\_index do |pancake, i|

Jan

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [February 2, 2009, 6:42pm UTC](https://www.ruby-forum.com/t/how-to-know-the-index-value-on-the-each-method/159241/4 "2009-02-02T18:42:08Z")

</div>

Thanks for the help. It was exactly what I needed. Thanks again,

ElÃ­as
