Last Updated: February 25, 2016
·
872
· otobrglez

Convert Hash keys to symbols

Extend Hash

class Hash
  def keys_to_symbols!
    self.keys.inject({}) { |i,k|
      i.merge!({k.to_sym => self.values_at(k).first}) }
  end
end

Use like this:

h = {"name" => "Oto", "twitter" => "otobrglez"}

And this...

puts h.keys_to_symbols!

... will produce this:

{:name => "Oto", :twitter => "otobrglez"}