Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8084195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:42:52+00:00 2026-06-05T17:42:52+00:00

Is the order of the values returned by Ruby’s Hash::each_value{ |val| block } somehow

  • 0

Is the order of the values returned by Ruby’s Hash::each_value{ |val| block } somehow defined? I.e. for a given hash is the order of values in the iterations of the loop always* the same?

I wonder if it is so, as the key-value pairs of hashes are not sorted unlike the values of an array.

Given the following example:

myhash = { :a => 100, :b => 200, :z => 9, :e => 101 }
myhash.each_value { |val|
  puts val
}

Does Ruby ensure it always* prints

100
200
9
101

Bonus question in case it is defined:
What are the prerequisites that the order is defined? Does it only apply for hard-coded hashes as the example?


[*]: “always” should be read as ‘on each execution and on any system with a working Ruby 1.9+‘.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:42:54+00:00Added an answer on June 5, 2026 at 5:42 pm

    In Ruby 1.8, Hashes are not ordered and the values will be returned in an arbitrary order that you cannot depend upon. In Ruby 1.9, Hashes are ordered based on the time the keys were inserted (first insertion positioned first, etc…).

    You specified in a footnote that “‘always’ should be read as ‘on each execution and on any system with a working Ruby 1.9+’.”. By that definition of “always”, the answer is “yes” 😉

    From the Ruby 1.8.7 Hash docs:

    The order in which you traverse a hash by either key or value may seem
    arbitrary, and will generally not be in the insertion order.

    From the Ruby 1.9.3 Hash docs:

    Hashes enumerate their values in the order that the corresponding keys were inserted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are the values returned by Javascript's getElementsByName(...) guaranteed to be in the same order
When you call dict.values() the order of the returned items is dependent on the
I want to order values from database query by date but I want also
Possible Duplicate: Ordering by the order of values in a SQL IN() clause With
Is there is a way to order by the order of the values in
Is it possible to order an NSMutableArray where it's values are NSStrings? I'm looking
SQL statement: INSERT INTO order (`ORDER_ID`,`SALE_CODE`,`CREATED_AT`,`UPDATED_AT`) VALUES ('2646253286','HPHS20','2009-07-11 12:07:40','2009-07-11 12:07:40') Error: You have an
I have an order form with about 30 text fields that contain numerical values.
I will be storing draw numbers (1-60) in fixed values and fixed order. One
I have a table and want to get 15 values with one order and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.