Best way to convert strings to symbols in hash

前端 未结 30 2990
借酒劲吻你
借酒劲吻你 2020-11-27 09:30

What\'s the (fastest/cleanest/straightforward) way to convert all keys in a hash from strings to symbols in Ruby?

This would be handy when parsing YAML.



        
30条回答
  •  难免孤独
    2020-11-27 10:09

    In case the reason you need to do this is because your data originally came from JSON, you could skip any of this parsing by just passing in the :symbolize_names option upon ingesting JSON.

    No Rails required and works with Ruby >1.9

    JSON.parse(my_json, :symbolize_names => true)
    

提交回复
热议问题