Class: MyJohnDeere::HashUtils
- Inherits:
-
Object
- Object
- MyJohnDeere::HashUtils
- Defined in:
- lib/myjohndeere/hash_utils.rb
Class Method Summary collapse
-
.deep_stringify_keys(in_hash) ⇒ Object
Convert keys to strings, recursively.
-
.stringify_keys(in_hash) ⇒ Object
Convert keys to strings.
- .transform_hash(original, options = {}, &block) ⇒ Object
Class Method Details
.deep_stringify_keys(in_hash) ⇒ Object
Convert keys to strings, recursively
27 28 29 30 31 |
# File 'lib/myjohndeere/hash_utils.rb', line 27 def self.deep_stringify_keys(in_hash) transform_hash(in_hash, :deep => true) {|hash, key, value| hash[key.to_s] = value } end |
.stringify_keys(in_hash) ⇒ Object
Convert keys to strings
20 21 22 23 24 |
# File 'lib/myjohndeere/hash_utils.rb', line 20 def self.stringify_keys(in_hash) transform_hash(in_hash) {|hash, key, value| hash[key.to_s] = value } end |
.transform_hash(original, options = {}, &block) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/myjohndeere/hash_utils.rb', line 3 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| transform_hash(v, , &block)} else value end end block.call(result,key,value) result } end |