Hash#values and Hash#keys order

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

  1. Alex Fenton

    Alex Fenton Guest

    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
     
    Alex Fenton, Apr 7, 2006
    #1
    1. Advertising

  2. Alex Fenton

    George Ogata Guest

    Alex Fenton <> 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
     
    George Ogata, Apr 15, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Menghan Zheng
    Replies:
    1
    Views:
    282
    alex23
    Apr 20, 2010
  2. Cameron Simpson
    Replies:
    6
    Views:
    364
    alex23
    Apr 21, 2010
  3. Mage

    hash.keys and hash.values

    Mage, Aug 13, 2006, in forum: Ruby
    Replies:
    14
    Views:
    191
  4. Ronald Fischer

    Hash#keys, Hash#values order question

    Ronald Fischer, Aug 23, 2007, in forum: Ruby
    Replies:
    0
    Views:
    171
    Ronald Fischer
    Aug 23, 2007
  5. Hemant Shah
    Replies:
    7
    Views:
    127
    Hemant Shah
    Jan 9, 2004
Loading...

Share This Page