Array#each iterates over an array from index 0 to #length - 1. So,
with the array

  [1, 3, 2]

Array#each would yield 1, then 3, then 2. Have a look at Array#sort:

  [1, 3, 2].sort { |a, b| a <=> b } # => [1, 2, 3]

On 5/31/06, Matthew Smillie <M.B.Smillie / sms.ed.ac.uk> wrote:
> On May 31, 2006, at 20:35, Wes Gamble wrote:
>
> > If I do
> >
> > Array.each do | arr_element |
> > ...
> > done
> >
> > am I guaranteed to get the array elements in order from lowest to
> > highest?
> >
> > Or should I use a for loop to get the array elements?
>
> I don't know if it's guaranteed (in the sense that it will never
> change), but that is what it seems to do:
>
> /*
>   *  call-seq:
>   *     array.each {|item| block }   ->   array
>   *
>   *  Calls <i>block</i> once for each element in <i>self</i>, passing
> that
>   *  element as a parameter.
>   *
>   *     a = [ "a", "b", "c" ]
>   *     a.each {|x| print x, " -- " }
>   *
>   *  produces:
>   *
>   *     a -- b -- c --
>   */
>
> VALUE
> rb_ary_each(ary)
>      VALUE ary;
> {
>      long i;
>
>      for (i=0; i<RARRAY(ary)->len; i++) {
>          rb_yield(RARRAY(ary)->ptr[i]);
>      }
>      return ary;
> }
>
>
>
>


-- 
- Simen