Created
May 12, 2016 13:29
-
-
Save mikecmpbll/6fde404d93df949d4f09264822a8a72d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def recursive_merge!(enum, enum2) | |
enum2.map do |k, v2| | |
v = enum[k] | |
enum[k] = | |
if v.is_a?(Hash) && v2.is_a?(Hash) | |
recursive_merge(v, v2) | |
elsif v.is_a?(Array) && v2.is_a?(Array) | |
v | v2 | |
else | |
v2 | |
end | |
end | |
enum | |
end | |
def recursive_merge(enum, enum2) | |
enum = enum.dup | |
recursive_merge!(enum, enum2) | |
end | |
a = { foo: [{ bar: 2 }], abc: 200, baz: [1,2,3,4] } | |
b = { foo: [{ bar: 5 }], abc: 12, baz: [5,6,7,8] } | |
recursive_merge(a, b) | |
# => {:foo=>[{:bar=>2}, {:bar=>5}], :abc=>12, :baz=>[1, 2, 3, 4, 5, 6, 7, 8]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment