Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/rest_connection/patches.rb
Overview
Hash Patches
Instance Method Summary collapse
-
#deep_merge(second) ⇒ Object
Merges self with another second, recursively.
-
#deep_merge!(second) ⇒ Object
From: www.gemtacular.com/gemdocs/cerberus-0.2.2/doc/classes/Hash.html File lib/cerberus/utils.rb, line 42 Modified to provide same functionality with Arrays.
Instance Method Details
#deep_merge(second) ⇒ Object
Merges self with another second, recursively.
This code was lovingly stolen from some random gem: gemjack.com/gems/tartan-0.1.1/classes/Hash.html
Thanks to whoever made it.
Modified to provide same functionality with Arrays
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rest_connection/patches.rb', line 36 def deep_merge(second) target = dup return target unless second unless Hash === second raise TypeError.new("can't convert #{second.class} into #{self.class}") end second.keys.each do |k| if second[k].is_a? Array and self[k].is_a? Array target[k] = target[k].deep_merge(second[k]) next elsif second[k].is_a? Hash and self[k].is_a? Hash target[k] = target[k].deep_merge(second[k]) next end target[k] = second[k] end target end |
#deep_merge!(second) ⇒ Object
From: www.gemtacular.com/gemdocs/cerberus-0.2.2/doc/classes/Hash.html File lib/cerberus/utils.rb, line 42 Modified to provide same functionality with Arrays
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rest_connection/patches.rb', line 59 def deep_merge!(second) return nil unless second unless Hash === second raise TypeError.new("can't convert #{second.class} into #{self.class}") end second.each_pair do |k,v| if self[k].is_a?(Array) and second[k].is_a?(Array) self[k].deep_merge!(second[k]) elsif self[k].is_a?(Hash) and second[k].is_a?(Hash) self[k].deep_merge!(second[k]) else self[k] = second[k] end end end |