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

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

T.

On May 31, 2006, at 20:35, Wes G. 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 block once for each element in self, 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]

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 May 31, 2006, at 2:44 PM, [email protected] 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 G. II

Wes G. wrote:

James G. wrote:

On May 31, 2006, at 2:44 PM, [email protected] 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 G. 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

James G. wrote:

On May 31, 2006, at 2:44 PM, [email protected] 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 G. 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

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

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

Wes G. 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