How to change hash keys from `Symbol`s to `String`s?

user12882 picture user12882 · May 11, 2012 · Viewed 46.3k times · Source

I am using Ruby on Rails 3.2.2 and I would like to "easily" / "quickly" change hash keys from Symbols to Strings. That is, from {:one => "Value 1", :two => "Value 2", ...} to {"one" => "Value 1", "two" => "Value 2", ...}.

How can I make that by using less code as possible?

Answer

Viktor Trón picture Viktor Trón · May 11, 2012

simply call stringify_keys (or stringify_keys!)

http://apidock.com/rails/Hash/stringify_keys