How do I convert a Ruby hash so that all of its keys are symbols?

后端 未结 15 1802
不思量自难忘°
不思量自难忘° 2020-12-04 19:01

I have a Ruby hash which looks like:

{ \"id\" => \"123\", \"name\" => \"test\" }

I would like to convert it to:

{ :id         


        
15条回答
  •  温柔的废话
    2020-12-04 19:52

    If you're using Rails (or just Active Support):

    { "id" => "123", "name" => "test" }.symbolize_keys
    

提交回复
热议问题