All posts

Convert ruby hash with string keys to symbol keys

 ruby

If you have a ruby hash with string / arrow keys like so:

{
  "key1" => "val1",
  "key2" => "val2"
}

But you want the hash to have symbol keys, you can use the transform_keys method:

{
  "key1" => "val1",
  "key2" => "val2"
}.transform_keys(&:to_sym)

=> {:key1=>"val1", :key2=>"val2"}

Further reading: