lib/core/ext/hash.rb in swagalicious-0.1.0 vs lib/core/ext/hash.rb in swagalicious-0.2.0
- old
+ new
@@ -1,10 +1,18 @@
class Hash
- def deep_merge!(other_hash)
- self.merge(other_hash) do |key, oldval, newval|
- oldval = oldval.to_hash if oldval.respond_to?(:to_hash)
- newval = newval.to_hash if newval.respond_to?(:to_hash)
- oldval.class.to_s == "Hash" && newval.class.to_s == "Hash" ? oldval.deep_merge(newval) : newval
+ def deep_merge(other_hash, &block)
+ dup.deep_merge!(other_hash)
+ end
+
+ def deep_merge!(other_hash, &block)
+ merge!(other_hash) do |key, this_val, other_val|
+ if this_val.is_a?(Hash) && other_val.is_a?(Hash)
+ this_val.deep_merge(other_val, &block)
+ elsif block_given?
+ block.call(key, this_val, other_val)
+ else
+ other_val
+ end
end
end
def slice(*keep_keys)
h = {}