ruby / psych

A libyaml wrapper for Ruby
MIT License
565 stars 204 forks source link

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

Open joallard opened 12 years ago

joallard commented 12 years ago

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"}
kendagriff commented 11 years ago

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
t3hk0d3 commented 11 years ago

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

foo:
   bar:
      hello:
         world: "yeah"