Hash#values and Hash#keys order

A

Alex Fenton

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
 
G

George Ogata

Alex Fenton said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top