Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Merge two Ruby hashes, with some special treatment to merge array values.
Ruby
branch: master
Failed to load latest commit information.
lib Don't merge nil into array.
test Don't merge nil into array.
README.md Added a README and a test to ensure it's correct.

README.md

Smart hash merge

Recursively merge two hashes, merging array values.

Examples

Values from the left hash are replaced by those from the right hash, or created if they do not exist:

a = { "overwritten" => "A" }
b = { "overwritten" => "B", "new" => "C" }

SmartHashMerge.merge(a, b)

# => { "overwritten" => "B", "new" => "C" }

Arrays are merged:

a = { "array" => [1, 2, 3] }
b = { "array" => [4, 5, 6] }

SmartHashMerge.merge(a, b)

# => { "array" => [1, 2, 3, 4, 5, 6] }

Single values merged with an array are merged into an array:

a = { "array" => 1 }
b = { "array" => [2, 3] }

SmartHashMerge.merge(a, b)

# => { "array" => [1, 2, 3] }

Merging is recursive:

a = { "foo" => { "bar" => "baz" } }
b = { "foo" => { "bar" => ["quux"] } }

SmartHashMerge.merge(a, b)

# => { "foo" => { "bar" => ["baz", "quux"] } }
Something went wrong with that request. Please try again.