Skip to content

Instantly share code, notes, and snippets.

@tcocca
Created May 12, 2010 16:42
Show Gist options
  • Select an option

  • Save tcocca/398805 to your computer and use it in GitHub Desktop.

Select an option

Save tcocca/398805 to your computer and use it in GitHub Desktop.
require 'active_support/core_ext/class/attribute_accessors'
require 'active_support/core_ext/array'
require 'active_support/inflector'
require 'active_support/core_ext/class/inheritable_attributes'
require 'active_support/core_ext/duplicable'
class Hash #:nodoc:
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)
if oldval.is_a?(Hash) && newval.is_a?(Hash)
oldval.deep_merge(newval)
elsif oldval.is_a?(Array) && newval.is_a?(Array)
oldval += newval
else
newval
end
end
end
def deep_merge!(other_hash)
replace(deep_merge(other_hash))
end
def deep_symbolize_keys
self.inject({}) { |result, (key, value)|
value = value.deep_symbolize_keys if value.is_a?(Hash)
result[(key.to_sym rescue key) || key] = value
result
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment