Best way to convert strings to symbols in hash

前端 未结 30 3006
借酒劲吻你
借酒劲吻你 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条回答
  •  Happy的楠姐
    2020-11-27 09:48

    Facets' Hash#deep_rekey is also a good option, especially:

    • if you find use for other sugar from facets in your project,
    • if you prefer code readability over cryptical one-liners.

    Sample:

    require 'facets/hash/deep_rekey'
    my_hash = YAML.load_file('yml').deep_rekey
    

提交回复
热议问题