Class: HashUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/hash.rb

Class Method Summary collapse

Class Method Details

.deep_stringify_keys(hash) ⇒ Object

Convert keys to strings, recursively



23
24
25
26
27
# File 'lib/hash.rb', line 23

def self.deep_stringify_keys(hash)
  transform_hash(hash, :deep => true) {|hash, key, value|
    hash[key.to_s] = value
  }
end

.transform_hash(original, options = {}, &block) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/hash.rb', line 2

def self.transform_hash(original, options={}, &block)
  original.inject({}){|result, (key,value)|
    value = if (options[:deep] && Hash === value)
              transform_hash(value, options, &block)
            else
              if Array === value
                value.map{|v|
                  if Hash === v
                    transform_hash(v, options, &block)
                  else
                    v
                  end}
              else
                value
              end
            end
    block.call(result,key,value)
    result
  }
end