psych icon indicating copy to clipboard operation
psych copied to clipboard

Add an option to parse hash keys as symbols instead of strings

Open joallard opened this issue 13 years ago • 2 comments

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"}

joallard avatar Apr 08 '12 08:04 joallard

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 avatar Jan 07 '13 14:01 kendagriff

@kendagriff It won't automagically convert folded hashes into HashWithIndifferentAccess

foo:
   bar:
      hello:
         world: "yeah"

t3hk0d3 avatar May 25 '13 11:05 t3hk0d3