Class: HashUtils
- Inherits:
-
Object
- Object
- HashUtils
- Defined in:
- lib/hash.rb
Class Method Summary collapse
-
.deep_stringify_keys(hash) ⇒ Object
Convert keys to strings, recursively.
- .transform_hash(original, options = {}, &block) ⇒ Object
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, ={}, &block) original.inject({}){|result, (key,value)| value = if ([:deep] && Hash === value) transform_hash(value, , &block) else if Array === value value.map{|v| if Hash === v transform_hash(v, , &block) else v end} else value end end block.call(result,key,value) result } end |