[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to use iterator for access to multiple elements?

Sven

9/25/2006 9:32:00 AM

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

3 Answers

Sam Gentle

9/25/2006 9:41:00 AM

0

On 9/25/06, Sven <sven@ast.dfs.de> 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

Sven

9/25/2006 10:13:00 AM

0


Sam Gentle 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

Sam Gentle

9/26/2006 4:44:00 AM

0

On 9/25/06, Sven <sven@ast.dfs.de> wrote:
> Sam Gentle 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?

Certainly. I use www.ruby-doc.org - specifically the "core api" and
"standard api" sections.

Sam