[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Hash#values and Hash#keys order

Alex Fenton

4/7/2006 1:14:00 PM

Hi

Are Hash#keys and Hash#values guaranteed to return the entries in the same order? i.e. is the following always true?

hash.values == hash.values_at(*hash.keys )

It seems to work, but can I depend on this behaviour?

thanks
alex
1 Answer

George Ogata

4/15/2006 5:45:00 AM

0

Alex Fenton <alex@deleteme.pressure.to> writes:

> Hi
>
> Are Hash#keys and Hash#values guaranteed to return the entries in
> the same order? i.e. is the following always true?
>
> hash.values == hash.values_at(*hash.keys )
>
> It seems to work, but can I depend on this behaviour?

In the current implementation, they will always be the same (assuming
the hash isn't modified between the #keys and #values calls). It
isn't documented AFAIK, so I don't know if you can depend on it in the
future. If you don't want to, perhaps you can use:

keys, values = *h.to_a.transpose