title: Ruby: next / previous values in array, loop array, array position
body: <p>Let's say I have an array of random numbers in no particular order. Let's say these are the ID #'s for people who ran in a marathon, and they're added to the array in the order they finish, something like:</p>
  
  <pre><code>race1 = [8, 102, 67, 58, 91, 16, 27]
  race2 = [51, 31, 7, 15, 99, 58, 22]
  </code></pre>
  
  <p>This is a simplified and somewhat contrived example, but I think it conveys the basic idea.</p>
  
  <p>Now a couple questions:</p>
  
  <p>First, how could I get the IDs that are before and after a particular entry? Let's say I am looking at runner 58 and I want to know who finished before and after him.</p>
  
  <pre><code>race1, runner58: previous finisher=67, next finisher=91
  race2, runner58: previous finisher=99, next finisher=22
  </code></pre>
  
  <p>Second, if I'm looking at the person who finished first or last, how can I have the "next" or "previous" loop back around the array?</p>
  
  <pre><code>race1, runner8: previous finisher=27, next finisher=102
  race2, runner22: previous finisher=58, next finisher=51
  </code></pre>
  
  <p>Lastly, I'd like to show what position each runner finished in. Given just the array and a value in it, how can I find out what 'ordinal' position it is in the array? Ie:</p>
  
  <pre><code>race1: runner8=1st, runner102=2nd, runner67=3rd ... runner27=last
  </code></pre>
  
  <p>Thanks very much!</p>