Best way to convert strings to symbols in hash

前端 未结 30 3005
借酒劲吻你
借酒劲吻你 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:04

    http://api.rubyonrails.org/classes/Hash.html#method-i-symbolize_keys

    hash = { 'name' => 'Rob', 'age' => '28' }
    hash.symbolize_keys
    # => { name: "Rob", age: "28" }
    

提交回复
热议问题