From: Rick DeNatale Date: 2008-07-05T00:31:01+09:00 Subject: Re: thoughts on a more generic Array#partition function ------=_Part_447_8801735.1215185657589 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On Fri, Jul 4, 2008 at 3:27 AM, Robert Klemme wrote: > 2008/7/4 Pe=F1a, Botp : > > From: Yukihiro Matsumoto [mailto:matz@ruby-lang.org] > > # writes: > > # |current test: > > # | > > # |1.9 not sorted > > # |1.8.6 not sorted > > # |1.8.7 sorted > > # | > > # |i prefer 1.8.7 default behaviour > > # Hmm, from my understanding, 1.9 preserves the order, prior version > > # does not. Could you show us the test you got the above conclusion? > > >> [RUBY_VERSION, RUBY_RELEASE_DATE, RUBY_REVISION] > > =3D> ["1.9.0", "2008-06-20", 17482] > > > >> RUBY_PLATFORM > > =3D> "i386-mswin32" > > > >> a =3D [ 1, 2, 3, 4, 7, 9, 11, 12, 13, 14 ] > > =3D> [1, 2, 3, 4, 7, 9, 11, 12, 13, 14] > > > >> a.group_by { |i| i % 3 } > > =3D> {1=3D>[1, 4, 7, 13], 2=3D>[2, 11, 14], 0=3D>[3, 9, 12]} > > > > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > > at this point, 1.9 and 1.8.7 differs > > Where exactly? I cannot seem to see it. Above and below look > identical. What am I missing? > > >> h=3Da.group_by { |i| i % 3 } > > =3D> {1=3D>[1, 4, 7, 13], 2=3D>[2, 11, 14], 0=3D>[3, 9, 12]} > > I get > > 09:29:30 bas$ ruby --version > ruby 1.8.7 (2008-06-20 patchlevel 22) [i386-cygwin] > 09:29:40 bas$ ruby -e 'p [ 1, 2, 3, 4, 7, 9, 11, 12, 13, 14 ].group_by > { |i| i % 3 }' > {0=3D>[3, 9, 12], 1=3D>[1, 4, 7, 13], 2=3D>[2, 11, 14]} > > 09:29:43 bas$ ruby19 --version > ruby 1.9.0 (2008-03-01 revision 15664) [i386-cygwin] > 09:29:47 bas$ ruby19 -e 'p [ 1, 2, 3, 4, 7, 9, 11, 12, 13, 14 > ].group_by { |i| i % 3 }' > {1=3D>[1, 4, 7, 13], 2=3D>[2, 11, 14], 0=3D>[3, 9, 12]} > > And there *is* a difference. > Yes there is. And I suspect that some following this thread are confused b= y the difference between sorted and ordered. Ruby 1.8 (at least before 1.8.7) gives no specification of the order in which a hash will yield it's keys, values or key-value pairs for methods in the each family. Ruby 1.9 keeps track of the INSERTION order so what we are seeing in the 1.= 9 output is a result of the fact that group_by inserts a key value pair each time it encounters a NEW value from the block., so with the array [1, 2, 3, ...] the block (which returns element % 3) will first return 1, then 2, the= n 0, after which all of the possible return values have been exhausted. So the hash is ordered by key as 1 =3D> ... , 2=3D> ..., 3=3D>... In the case of Ruby 1.8, the enumeration order is an accident of the way th= e key values hash, since the hash value of a fixnum n in MRI Ruby 1.8 is n*2+1, in this case the enumeration order is somewhat predictable. But in general hashes are not enumerated in key-sort order in any version of Ruby as far as I know. --=20 Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/ ------=_Part_447_8801735.1215185657589--