my_hash = { key1: 'value', key2: 'value', key3: 'value' }
my_hash = my_hash.order(:key2, :key1, :key3)
puts my_hash
=> { key2: 'value', key1: 'value', key3: 'value' }
class Hashdef order(*keys)Hash[keys.collect {|k| self[k] ? [k, self[k]] : nil }.compact]endend
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/285cec00-c279-405b-982e-0710501bcb68%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Hashes are by definition unordered. If you think you need an ordered hash, then you probably don't understand hashes...