Newbie: Does Array.each guarantee an ordered result?

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?

Thanks,
Wes

···

--
Posted via http://www.ruby-forum.com/.

They are the same. A for-loop is "translated" into an each-loop.

T.

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

···

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?

  *
  * 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;
}

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

They are the same. A for-loop is "translated" into an each-loop.

Well, almost:

>> things = [1, 2, 3]
=> [1, 2, 3]
>> things.each { |thing| }
=> [1, 2, 3]
>> thing
NameError: undefined local variable or method `thing' for main:Object
         from (irb):3
>> for thing in things
>> # ...
?> end
=> [1, 2, 3]
>> thing
=> 3

James Edward Gray II

···

On May 31, 2006, at 2:44 PM, transfire@gmail.com wrote:
         from :0

yah -- and since each is almost the same thing as for, i.e., for is
powered by each, you would want to do something like

[1,3,2].sort.each

which should give you 1, then 2, then 3.

···

On 5/31/06, Simen Edvardsen <toalett@gmail.com> wrote:

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

--
Giles Bowkett
http://www.gilesgoatboy.org

James Gray wrote:

···

On May 31, 2006, at 2:44 PM, transfire@gmail.com wrote:

They are the same. A for-loop is "translated" into an each-loop.

Well, almost:

>> things = [1, 2, 3]
=> [1, 2, 3]
>> things.each { |thing| }
=> [1, 2, 3]
>> thing
NameError: undefined local variable or method `thing' for main:Object
         from (irb):3
         from :0
>> for thing in things
>> # ...
?> end
=> [1, 2, 3]
>> thing
=> 3

James Edward Gray II

Valid. But as long as you're not interested in manipulating that last
element outside of the loop, they're functionally equivalent.

Thanks for everyone for the good info.

Wes

--
Posted via http://www.ruby-forum.com/\.

Wes Gamble wrote:

James Gray wrote:

They are the same. A for-loop is "translated" into an each-loop.

Well, almost:

>> things = [1, 2, 3]
=> [1, 2, 3]
>> things.each { |thing| }
=> [1, 2, 3]
>> thing
NameError: undefined local variable or method `thing' for main:Object
         from (irb):3
         from :0
>> for thing in things
>> # ...
?> end
=> [1, 2, 3]
>> thing
=> 3

James Edward Gray II

Valid. But as long as you're not interested in manipulating that last
element outside of the loop, they're functionally equivalent.

Thanks for everyone for the good info.

Wes

By the way, I just realized - I meant that I wanted the array elements
in subscript lowest to highest order (in their natural array order), NOT
that I wanted to sort the values. My language was sloppy in the OP.

I just wanted to know if each was basically a hash iterator and if so,
did I have to be concerned about a hash not having any implicit
ordering.

Put another way, by using the each method on an array, was I "giving up"
the implied ordering of the array so that I might not get the array
elements in the order in which they existed in the array.

Wes

···

On May 31, 2006, at 2:44 PM, transfire@gmail.com wrote:

--
Posted via http://www.ruby-forum.com/\.

Wes Gamble wrote:

Valid. But as long as you're not interested in manipulating that last element outside of the loop, they're functionally equivalent.

Yes... but more generally you should realize that a block
opens a new scope, where a for loop does not.

Hal

Array#each does not change the order of the array.

j`ey
http://www.eachmapinject.com

···

On 5/31/06, Wes Gamble <weyus@att.net> wrote:

By the way, I just realized - I meant that I wanted the array elements
in subscript lowest to highest order (in their natural array order), NOT
that I wanted to sort the values. My language was sloppy in the OP.

I just wanted to know if each was basically a hash iterator and if so,
did I have to be concerned about a hash not having any implicit
ordering.

Put another way, by using the each method on an array, was I "giving up"
the implied ordering of the array so that I might not get the array
elements in the order in which they existed in the array.

Wes

--
Posted via http://www.ruby-forum.com/\.