Ruby: next / previous values in array, loop array, array position -


Assume that I do not have a random number in a particular order. Suppose that they are ID # for the participants in the marathon, and the orders they are included are added in order:

  race1 = [8, 102, 67] , 58, 91, 16, 27] Race 2 = [51, 31, 7, 15, 99, 58, 22]   

This is a simplified and somewhat customized example, but I think it tells the original idea.

Now a few questions:

First, how can I identify before and after a specific entry? We say that I am looking at 58 runners and I know who has ended before and after him.

  race1, runner58: last match = 67, next finisher = 91 race 2, runner 58: 99, next finisher = 22   

second, if I I am looking at the person who is either first or last, then how do I "next" or "previous" last finisher = 58, next Finisher = 51

Finally, I want to show this That how the status of each runner has ended. Looking at just the array and a value in it, how can I find out what 'sequential' situation is in the array? Ie:

  race1: runner8 = 1, runner102 = 2nd, runner = 3 ... runner27 = final   

Thanks a lot!

First & amp; Second:

  index = race 1. Fund_Intix (58) If! Index. "# {Race1 [index-1], # {race1 [index 1], race 1 [0]}" end   

Finally:

Gem Installed Linguistics

Then

  Language science is required 'Linguistics :: Usage (: en) race1.each_with_index {| Runner, ii "Runner # puts {runner} = # {(i + 1) .en.ordinal}"}    

Comments