psych
psych copied to clipboard
Add an option to parse hash keys as symbols instead of strings
This is a feature request. I like symbols. An argument could be made about object duplication and so on, but I just like them because they are easier to type. It would be nice to have an option to parse hash keys as symbols.
For example:
irb(main):001:0> Psych.load("---\n ananas: sweet\n bananas: yellow")
=> {"ananas"=>"sweet", "bananas"=>"yellow"}
irb(main):002:0> Psych.load("---\n ananas: sweet\n bananas: yellow", :symbolize_keys => true)
=> {:ananas=>"sweet", :bananas=>"yellow"}
You could always use HashWithIndifferentAccess from the ActiveSupport library, by calling with_indifferent_access:
require 'active_support/core_ext/hash'
Psych.load("---\n ananas: sweet\n bananas: yellow").with_indifferent_access
@kendagriff
It won't automagically convert folded hashes into HashWithIndifferentAccess
foo:
bar:
hello:
world: "yeah"