Module: RecursiveHashUtils
- Defined in:
- lib/recursive-hash-utils.rb
Overview
Helper methods to recursively modify Hash objects
Class Method Summary collapse
-
.convert_string_keys_to_symbols(hash) ⇒ Object
Recursively convert all string keys in a Hash to symbol keys.
Class Method Details
.convert_string_keys_to_symbols(hash) ⇒ Object
Recursively convert all string keys in a Hash to symbol keys. Will recurse through nested Hash and Array objects.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/recursive-hash-utils.rb', line 6 def self.convert_string_keys_to_symbols hash s2s = lambda do |h| if Hash === h Hash[ h.map do |k, v| [k.respond_to?(:to_sym) ? k.to_sym : k, s2s[v]] end ] elsif Array === h hash_array = [] h.each do |val| hash_array.push s2s[val] end hash_array else h end end s2s[hash] end |