On 14.08.2006 17:06, Mage wrote:
> Chad Perrin wrote:
>>
>> I suspect people don't even get as far as taking it for granted, since
>> they probably tend to access one from the other in almost all cases.
>>   
> On the second week of my Ruby "experience" I found myself writing a 
> method (for a very basic db layer) where hash.keys and hash.values could 
> come in play.
> So I think it's a natural need. After a short Google session I found 
> others dealing with this, most of them assumed that they have same order.
> 
> Based on the answers of this thread I believe they are, however until it 
> becomes documented I will use hash.to_a.transpose in production 
> environment.

Here's another solution - maybe it's even more efficient as it doesn't 
need the transposing:

 >> hash={1=>2,3=>4}
=> {1=>2, 3=>4}
 >> keys,vals = hash.inject([[],[]]) {|(ks,vs),(k,v)| [ks << k, vs << v]}
=> [[1, 3], [2, 4]]
 >> keys
=> [1, 3]
 >> vals
=> [2, 4]

... of course using #inject. :-)

Kind regards

	robert