Home

How To Reverse a Ruby Hash

It's nice and easy to reverse a ruby array. See how to easily convert a hash as well.

It's easy to reverse a Ruby array.

a = %w{ red blue green }
# => ["red", "blue", "green"]

a.reverse
# => ["green", "blue", "red"]

But it's not as straightforward with a hash. We have to convert it to an array, reverse it, and send it back to a hash. This works for nested hashes as well.

my_hash = {
:key_1 => 1,
:key_2 => 2,
:key_3 => [1, 2, 3],
:key_4 => {
:subkey_1 => 1,
:subkey_2 => 2
}
}
# => {:key_1=>1, :key_2=>2, :key_3=>[1, 2, 3], :key_4=>{:subkey_1=>1, :subkey_2=>2}}

new_hash = Hash[my_hash.to_a.reverse].to_hash
# => {:key_4=>{:subkey_1=>1, :subkey_2=>2}, :key_3=>[1, 2, 3], :key_2=>2, :key_1=>1}

If you want to keep it simple, you can add a method to the Hash class.

class Hash

def reverse
Hash[self.to_a.reverse]
end

end

Then you could do this:

my_hash.reverse
# => {:key_4=>{:subkey_1=>1, :subkey_2=>2}, :key_3=>[1, 2, 3], :key_2=>2, :key_1=>1}

Let's Connect

Keep Reading

Scrape and Parse A Webpage with Ruby (using Nokogiri)

A quick way to pull content from a URL and parse it using Ruby.

Feb 23, 2016

Find the Source of a Ruby Method's "Super"

It can be helpful when debugging to know what "super" is actually calling.

Oct 12, 2016

Don't Overthink Slugs in Ruby/Rails

It can take some tricky logic to transform unpredictable characters into a URL-friendly string. But with Rails, you don't need to worry about.

Jul 11, 2018