From: Stephen White Date: 2001-07-15T04:47:30+09:00 Subject: [ruby-talk:17852] Ruby Idioms While playing around with the idea of ordered Hashs, I wrote the following to sort the values into key order. ahash = Hash[*(1..50).map { rand(100) }] # Create something to sort. one = ahash.keys.sort.map {|i| ahash[i]} two = ahash.sort {|i,j| i[0] <=> j[0]}.map {|i,j| j} p one == two -=> true The first method gets the list of keys, sorts them, then re-accesses the variable. The second sorts the array in place then strips out the keys. Which is the better method? I have a feeling that Smalltalkers and functional programmers would prefer the second method, but I find the first method more concise and easier to understand. The first method seems a uniquely Ruby construct. It's a combination of OO (ahash produces keys) and procedural (ahash is operated on to get the values) that only works because they can communicate through the iterator. -- spwhite@chariot.net.au