Arnold Roa
Arnold Roa

Reputation: 7738

Getting hash values as an array keeping order in ruby 1.8.3

I'm doing:

{:a => 'hello', :c => 'lovely', :b => 'word'}.values

It returns:

[0] "hello",
[1] "word",
[2] "lovely"

Why did the order change? Any way to easily return the values in the order they were defined in hash?

Upvotes: 0

Views: 156

Answers (1)

fylooi
fylooi

Reputation: 3870

Rails implements an OrderedHash for Ruby version < 1.9. You can consider implementing it if you're not using Rails.

http://apidock.com/rails/ActiveSupport/OrderedHash

Upvotes: 3

Related Questions