https://github.com/danielsdeleo/deep_merge
Recursive Merging for Ruby Hashes
https://github.com/danielsdeleo/deep_merge
Keywords from Contributors
rubygems activerecord activejob mvc brakeman pry security-audit security-tools debugger security-vulnerability
Last synced: about 1 hour ago
JSON representation
Repository metadata
Recursive Merging for Ruby Hashes
- Host: GitHub
- URL: https://github.com/danielsdeleo/deep_merge
- Owner: danielsdeleo
- License: mit
- Created: 2009-12-25T22:57:09.000Z (almost 16 years ago)
- Default Branch: master
- Last Pushed: 2025-05-27T15:39:48.000Z (7 months ago)
- Last Synced: 2025-11-19T00:27:30.155Z (24 days ago)
- Language: Ruby
- Homepage:
- Size: 84 KB
- Stars: 166
- Watchers: 7
- Forks: 61
- Open Issues: 12
- Releases: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG
- License: LICENSE
README.md
DeepMerge
Deep Merge is a simple set of utility functions for Hash. It permits you to merge elements inside a hash together recursively. The manner by which it does this is somewhat arbitrary (since there is no defining standard for this) but it should end up being pretty intuitive and do what you expect.
You can learn a lot more about this by reading the test file. It's pretty well documented and has many examples of various merges from very simple to pretty complex.
The primary need that caused me to write this library is the merging of elements coming from HTTP parameters and related stored parameters in session. This lets a user build up a set of parameters over time, modifying individual items.
DeepMerge Core Documentation
The deep_merge! method permits merging of arbitrary child elements. The two top level elements must be hashes. These hashes can contain unlimited (to stack limit) levels of child elements. These child elements do not have to be of the same type. Where child elements are of the same type, deep_merge will attempt to merge them together. Where child elements are not of the same type, deep_merge will skip or optionally overwrite the destination element with the contents of the source element at that level. For example,
source = {:x => [4, 5, '6'], :y => 2}
dest = {:x => [1, 2, 3], :y => [7, 8, 9]}
dest.deep_merge!(source)
# => {:x => [1, 2, 3, 4, 5, '6'], :y => 2}
By default, deep_merge! will overwrite any unmergeables and merge everything else. To avoid this, use deep_merge (no bang/exclamation mark).
Options
Options are specified in the last parameter passed, which should be in hash format
hash.deep_merge!({:x => [1, 2]}, {:knockout_prefix => '--'})
| Option | Default | Description |
|---|---|---|
:preserve_unmergeables |
false |
Set to true to skip any unmergeable elements from source |
:knockout_prefix |
nil |
Set to string value to signify prefix which deletes elements from existing element |
:overwrite_arrays |
false |
Set to true if you want to avoid merging arrays |
:unpack_arrays |
nil |
Set to string value to run Array.join then String.split against all arrays |
:sort_merged_arrays |
false |
Set to true to sort all arrays that are merged together |
:merge_hash_arrays |
false |
Set to true to merge hashes within arrays |
:extend_existing_arrays |
false |
Set to true to extend existing arrays, instead of overwriting them |
:keep_array_duplicates |
false |
Set to true to keep duplicate entries in arrays, instead of coalescing them |
:merge_nil_values |
false |
Set to true to merge nil hash values, overwriting a possibly non-nil value |
:merge_debug |
false |
Set to true to get console output of merge process for debugging |
:knockout_prefix
Provides a way to remove elements from an existing Hash by specifying them in a special way in the incoming hash.
source = {:x => ['--1', '3']}
dest = {:x => ['1', '2']}
dest.deep_merge!(source, :knockout_prefix => "--")
# => {:x => ['2', '3']}
Additionally, if the knockout_prefix is passed alone as a string, it will cause the entire element to be removed.
source = {:x => '--'}
dest = {:x => [1, 2, 3]}
dest.deep_merge!(source, :knockout_prefix => "--")
# => {:x => ""}
Note that the method ko_deep_merge! defaults the knockout prefix to "--" for convenience.
source = {:x => ['--1', '3']}
dest = {:x => ['1', '2']}
dest.ko_deep_merge!(source)
# => {:x => ['2', '3']}
:overwrite_arrays
Provides a way to replace Arrays instead of having them merge together.
source = {:x => ['1', '2']}
dest = {:x => ['3', '4']}
dest.deep_merge!(source, :overwrite_arrays => true)
# => {:x => ['1', '2']}
:unpack_arrays
Permits compound elements to be passed in as strings and to be converted into discrete array elements.
source = {:x => ['5', '6', '7,8']}
dest = {:x => ['1,2,3', '4']}
dest.deep_merge!(source, :unpack_arrays => ',')
# => {:x => ['1', '2', '3', '4', '5', '6', '7', '8'}
The original purpose for this option is when receiving data from an HTML form, this makes it easy for a checkbox to pass multiple values from within a single HTML element.
:merge_hash_arrays
Merges hashes within arrays.
source = {:x => [{:z => 2}]}
dest = {:x => [{:y => 1}]}
dest.deep_merge!(source, :merge_hash_arrays => true)
# => {:x => [{:y => 1, :z => 2}]}
:extend_existing_arrays
Adds source elements to existing arrays, instead of overwriting them.
source = {:x => "4" }
dest = {:x => ["1", "2", "3"]}
dest.deep_merge!(source, :extend_existing_arrays => true)
# => {:x => ["1", "2", "3", "4"]}
:keep_array_duplicates
Keeps duplicate entries in arrays, instead of coalescing them.
By default, without this option, duplicate entries are coalesced.
source = {:x => ["2", "3"]}
dest = {:x => ["1", "2"]}
dest.deep_merge!(source)
# => {:x => ["1", "2", "3"]}
With this option they are kept.
source = {:x => ["2", "3"]}
dest = {:x => ["1", "2"]}
dest.deep_merge!(source, :keep_array_duplicates => true)
# => {:x => ["1", "2", "2", "3"]}
:merge_nil_values
Allows nil hash values to be merged.
By default, without this option, nil hash values in the source are ignored.
source = {:x => nil}
dest = {:x => "1"}
dest.deep_merge!(source)
# => {:x => "1"}
With this option, nil values will overwrite existing values.
source = {:x => nil}
dest = {:x => "1"}
dest.deep_merge!(source, :merge_nil_values => true)
# => {:x => nil}
Using deep_merge in Rails
To avoid conflict with ActiveSupport, load deep_merge via:
require 'deep_merge/rails_compat'
In a Gemfile:
gem "deep_merge", :require => 'deep_merge/rails_compat'
The deep_merge methods will then be defined as
Hash#deeper_merge
Hash#deeper_merge!
Hash#ko_deeper_merge!
Availability
deep_merge was written by Steve Midgley, and is now maintained by Daniel DeLeo. The official home of deep_merge on the internet is now https://github.com/danielsdeleo/deep_merge
License
Copyright (c) 2008-2016 Steve Midgley, released under the MIT license
Owner metadata
- Name: Dan DeLeo
- Login: danielsdeleo
- Email:
- Kind: user
- Description:
- Website:
- Location: Seattle, WA
- Twitter:
- Company: @chef
- Icon url: https://avatars.githubusercontent.com/u/37162?v=4
- Repositories: 70
- Last ynced at: 2023-04-10T07:28:03.761Z
- Profile URL: https://github.com/danielsdeleo
GitHub Events
Total
- Watch event: 2
- Issue comment event: 10
- Push event: 1
- Pull request event: 1
- Pull request review event: 2
- Pull request review comment event: 2
- Fork event: 3
Last Year
- Watch event: 1
- Issue comment event: 9
- Push event: 1
- Pull request event: 1
- Fork event: 3
Committers metadata
Last synced: 8 days ago
Total Commits: 63
Total Committers: 18
Avg Commits per committer: 3.5
Development Distribution Score (DDS): 0.698
Commits in past year: 1
Committers in past year: 1
Avg Commits per committer in past year: 1.0
Development Distribution Score (DDS) in past year: 0.0
| Name | Commits | |
|---|---|---|
| Jason Frey | f****9@g****m | 19 |
| Daniel DeLeo | d****n@o****m | 18 |
| Joe Van Dyk | j****e@f****m | 4 |
| Michael Sievers | m****s@w****e | 4 |
| Andrew Williams | s****u@g****m | 2 |
| Joe Rafaniello | j****e@r****m | 2 |
| Ken Dreyer | k****r@k****m | 2 |
| Michael Grosser | m****l@g****t | 2 |
| Niklas Lönn | n****n@e****e | 1 |
| Andrew Lipscomb | l****w@g****m | 1 |
| Ben Langfeld | b****n@l****e | 1 |
| Chris Johnson | c****s@k****m | 1 |
| Jared Beck | j****d@j****m | 1 |
| Jonathan Weiss | jw@i****e | 1 |
| Koichi ITO | k****o@g****m | 1 |
| Masahiro | w****e@c****m | 1 |
| mohammad-nabeel | 7****l | 1 |
| rrottmann | r****r@r****t | 1 |
Committer domains:
- rottmann.it: 1
- cadenza-tech.com: 1
- innerewut.de: 1
- jaredbeck.com: 1
- kindkid.com: 1
- langfeld.me: 1
- ebay-kleinanzeigen.de: 1
- grosser.it: 1
- ktdreyer.com: 1
- redhat.com: 1
- fixieconsulting.com: 1
- opscode.com: 1
Issue and Pull Request metadata
Last synced: 13 days ago
Total issues: 20
Total pull requests: 43
Average time to close issues: over 1 year
Average time to close pull requests: 8 months
Total issue authors: 19
Total pull request authors: 26
Average comments per issue: 2.95
Average comments per pull request: 2.19
Merged pull request: 24
Bot issues: 0
Bot pull requests: 0
Past year issues: 0
Past year pull requests: 2
Past year average time to close issues: N/A
Past year average time to close pull requests: 3 months
Past year issue authors: 0
Past year pull request authors: 1
Past year average comments per issue: 0
Past year average comments per pull request: 3.0
Past year merged pull request: 2
Past year bot issues: 0
Past year bot pull requests: 0
Top Issue Authors
- joevandyk (2)
- s-ga (1)
- firedev (1)
- owst (1)
- ankilosaurus (1)
- NicoLugil (1)
- mrbobbytables (1)
- robmbrooks (1)
- jay4git (1)
- jpb (1)
- stevendelarwelle (1)
- markussiebert (1)
- kke (1)
- mvimercati (1)
- kmile (1)
Top Pull Request Authors
- Fryguy (7)
- grosser (3)
- ktdreyer (3)
- santib (2)
- w-masahiro-ct (2)
- jrafanie (2)
- ameir (2)
- davidashman (2)
- kindkid (2)
- danielsdeleo (2)
- jaredbeck (1)
- dtaniwaki (1)
- hamdyelbatal122 (1)
- sobakasu (1)
- EricLarson2020 (1)
Top Issue Labels
Top Pull Request Labels
Package metadata
- Total packages: 4
-
Total downloads:
- rubygems: 210,767,652 total
- Total docker downloads: 334,925,650
- Total dependent packages: 239 (may contain duplicates)
- Total dependent repositories: 4,874 (may contain duplicates)
- Total versions: 18
- Total maintainers: 5
gem.coop: deep_merge
Recursively merge hashes.
- Homepage: https://github.com/danielsdeleo/deep_merge
- Documentation: http://www.rubydoc.info/gems/deep_merge/
- Licenses: MIT
- Latest release: 1.2.2 (published almost 4 years ago)
- Last Synced: 2025-12-10T03:32:05.210Z (3 days ago)
- Versions: 8
- Dependent Packages: 0
- Dependent Repositories: 0
- Downloads: 105,424,232 Total
- Docker Downloads: 167,462,825
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 0.181%
- Downloads: 0.237%
- Docker downloads count: 0.489%
- Maintainers (4)
rubygems.org: deep_merge
Recursively merge hashes.
- Homepage: https://github.com/danielsdeleo/deep_merge
- Documentation: http://www.rubydoc.info/gems/deep_merge/
- Licenses: MIT
- Latest release: 1.2.2 (published almost 4 years ago)
- Last Synced: 2025-12-09T04:31:31.207Z (4 days ago)
- Versions: 8
- Dependent Packages: 239
- Dependent Repositories: 4,874
- Downloads: 105,337,140 Total
- Docker Downloads: 167,462,825
-
Rankings:
- Dependent packages count: 0.164%
- Downloads: 0.254%
- Dependent repos count: 0.454%
- Docker downloads count: 0.593%
- Average: 1.596%
- Forks count: 3.618%
- Stargazers count: 4.493%
- Maintainers (4)
gem.coop: deep_merge2
Recursively merge hashes. Now works with Ruby 1.9 and ActiveSupport
- Homepage: https://github.com/danielsdeleo/deep_merge
- Documentation: http://www.rubydoc.info/gems/deep_merge2/
- Licenses: MIT
- Latest release: 1.0 (published over 9 years ago)
- Last Synced: 2025-12-09T19:04:07.115Z (3 days ago)
- Versions: 1
- Dependent Packages: 0
- Dependent Repositories: 0
- Downloads: 3,140 Total
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 28.737%
- Downloads: 86.211%
- Maintainers (1)
rubygems.org: deep_merge2
Recursively merge hashes. Now works with Ruby 1.9 and ActiveSupport
- Homepage: https://github.com/danielsdeleo/deep_merge
- Documentation: http://www.rubydoc.info/gems/deep_merge2/
- Licenses: MIT
- Latest release: 1.0 (published over 9 years ago)
- Last Synced: 2025-12-09T19:04:07.103Z (3 days ago)
- Versions: 1
- Dependent Packages: 0
- Dependent Repositories: 0
- Downloads: 3,140 Total
-
Rankings:
- Forks count: 3.231%
- Stargazers count: 4.161%
- Dependent packages count: 15.706%
- Average: 31.579%
- Dependent repos count: 46.782%
- Downloads: 88.013%
- Maintainers (1)
Dependencies
- rake ~> 10.1 development
- test-unit-minitest >= 0 development
- actions/checkout v2 composite
- ruby/setup-ruby v1 composite
Score: 28.18973351412002