Hash#values and Hash#keys order

Discussion in 'Ruby' started by Alex Fenton, Apr 7, 2006.

  1. Alex Fenton

    Alex Fenton Guest


    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?

    Alex Fenton, Apr 7, 2006
    1. Advertisements

  2. Alex Fenton

    George Ogata Guest

    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
    George Ogata, Apr 15, 2006
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.