From: Martin DeMello Date: 2004-08-17T14:50:59+09:00 Subject: Re: Q: Shifting a hash anf and array Meino Christian Cramer wrote: > > Contents of the array before the trick: > > 1 2 3 4 5 6 7 8 9 0 index > A B C D E F G H I J contents > > (execute *trick*) > > Contents of the array after the trick: > > 1 2 3 4 5 6 7 8 9 0 index > J A B C D E F G H I contents array.unshift(array.pop) > And now the shift of an hash: > > Contents of the hash before the trick: > > A B C D E F G H I J key > 1 2 3 4 5 6 7 8 9 0 value > > (execute *trick*) > > Contents of the hash after the trick: > > A B C D E F G H I J key > 0 1 2 3 4 5 6 7 8 9 value > > Both are very similiar. Not really :) Hashes are inherently unordered, so the concept of a rotation is a bit trickier. This works on the assumption that Hash#values returns values in positions corresponding to the keys in Hash#keys... class Array def rot1 unshift(pop) end end Hash[*(hash.keys.rot1.zip(hash.values.rot1).flatten)] martin