From: Wes Gamble Date: 2006-06-01T06:22:01+09:00 Subject: Re: Newbie: Does Array.each guarantee an ordered result? Wes Gamble wrote: > 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 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/.