# How to use iterator for access to multiple elements?

**URL:** <https://www.ruby-forum.com/t/how-to-use-iterator-for-access-to-multiple-elements/69898>\
**Category:** Ruby\
**Created:** [September 25, 2006, 11:35am UTC](https://www.ruby-forum.com/t/how-to-use-iterator-for-access-to-multiple-elements/69898 "2006-09-25T11:35:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47c2d/32.png) [@sven](https://www.ruby-forum.com/u/sven)\
**Post date:** [September 25, 2006, 11:35am UTC](https://www.ruby-forum.com/t/how-to-use-iterator-for-access-to-multiple-elements/69898/1 "2006-09-25T11:35:50Z")

</div>

Hi,

I want to write a function that calculate the time difference between  
two following elements in an array. But then I need access to both  
elements in this array.

When I use an iterator I only get the actual element of the array:

array.each {|element|

# previous = element.previous()

element.delay = element.time - previous.time  
}

Can someone tell me, how I could implement this function in a clever  
way?  
(A for-loop would also work, but that is not as smart as it is used to  
be in Ruby.)

Thanks,  
Sven

---

<div class="post-metadata">

**Author:** ![sven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47c2d/32.png) [@sven](https://www.ruby-forum.com/u/sven)\
**Post date:** [September 25, 2006, 11:41am UTC](https://www.ruby-forum.com/t/how-to-use-iterator-for-access-to-multiple-elements/69898/2 "2006-09-25T11:41:51Z")

</div>

On 9/25/06, Sven [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> I want to write a function that calculate the time difference between  
> two following elements in an array. But then I need access to both  
> elements in this array.

Have a look at Enumerator#each\_slice and Enumerator#each\_cons

Sam

---

<div class="post-metadata">

**Author:** ![sven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47c2d/32.png) [@sven](https://www.ruby-forum.com/u/sven)\
**Post date:** [September 25, 2006, 12:15pm UTC](https://www.ruby-forum.com/t/how-to-use-iterator-for-access-to-multiple-elements/69898/3 "2006-09-25T12:15:53Z")

</div>

Sam G. wrote:

> Have a look at Enumerator#each\_slice and Enumerator#each\_cons

Thanks, each\_cons should be the right one for me.

But where could I find this information on my own? Only with using your  
information for a web search I was able to find a little information  
about it.  
Also rubyforge does not help.  
There has to be an easy entry point for such information that I did not  
find yet. Is there any hint where I could start searching?

Thanks,  
Sven

---

<div class="post-metadata">

**Author:** ![sven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47c2d/32.png) [@sven](https://www.ruby-forum.com/u/sven)\
**Post date:** [September 26, 2006, 6:45am UTC](https://www.ruby-forum.com/t/how-to-use-iterator-for-access-to-multiple-elements/69898/4 "2006-09-26T06:45:37Z")

</div>

On 9/25/06, Sven [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> find yet. Is there any hint where I could start searching?  
> Certainly. I use [www.ruby-doc.org](http://www.ruby-doc.org) - specifically the “core api” and  
> “standard api” sections.

Sam
