Easy Diff enhances the functionality of Hash, allowing recursive diff, merge, and unmerge of arbitrarily constructed hashes. This is perfect for people who need to do diffs on not only plain text files but also data as Hash or JSON objects. Unmerge is included with diff and merge to more easily allow versioning of arbitrary data.
gem install easy_diff
Takes another hash and recursively determines the differences between the two. This method returns two hashes. The first contains what has to be removed from self to create the second hash. The second contains what has to be added.
original = { :tags => ['a', 'b', 'c'], :pos => {:x => '1', :y => '2'}, :some_str => "bla", :some_int => 1, :some_bool => false, :extra_removed => "bye" } modified = { :tags => ['b', 'c', 'd'], :pos => {:x => '3', :y => '2'}, :some_str => "bla", :some_int => 2, :some_bool => true, :extra_added => "hi" } removed, added = original.easy_diff modified # The removed and added hashes should contain the following: # removed = { # :tags => ['a'], # :pos => {:x => '1'}, # :some_int => 1, # :some_bool => false, # :extra_removed => "bye" # } # # added = { # :tags => ['d'], # :pos => {:x => '3'}, # :some_int => 2, # :some_bool => true, # :extra_added => "hi" # }
Takes a hash and recursively merges it with self. Arrays are merged by the union operation and then sorted. Using Hash#easy_merge! will modify self instead of returning a new hash.
original = { :tags => ['a', 'b', 'c'], :pos => {:x => '1', :y => '2'}, :some_str => "bla", :some_int => 1, :some_bool => false, :extra_removed => "bye" } extra = { :tags => ['d'], :pos => {:x => '3'}, :some_int => 2, :some_bool => true, :extra_added => "hi" } merged = original.easy_merge extra # The merged hash should look like this: # merged = { # :tags => ['a', 'b', 'c', 'd'], # :pos => {:x => '3', :y => '2'}, # :some_str => "bla", # :some_int => 2, # :some_bool => true, # :extra_removed => "bye", # :extra_added => "hi" # }
Takes a hash and recursively unmerges it with self. By unmerging, I mean it will remove all matching values from the hash. All matching elements will be removed in arrays as well and then the arrays will be sorted. Using Hash#easy_unmerge! will modify self instead of returning a new hash.
original = { :tags => ['b', 'c', 'd'], :pos => {:x => '3', :y => '2'}, :some_str => "bla", :some_int => 2, :some_bool => true, :extra_added => "hi" } extra = { :tags => ['d'], :pos => {:x => '3'}, :some_int => 2, :some_bool => true, :extra_added => "hi" } unmerged = original.easy_unmerge extra # The unmerged hash should look like this: # unmerged = { # :tags => ['b', 'c'], # :pos => {:y => '2'}, # :some_str => "bla" # }
Performs a deep clone on a hash
original = { :tags => ['b', 'c', 'd'], :pos => {:x => '3', :y => '2'} } new = original.easy_clone new[:tags] << 'e' new[:pos][:y] = '1' # The original hash will still look like this: # original = { # :tags => ['b', 'c', 'd'], # :pos => {:x => '3', :y => '2'} # }
-
Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet
-
Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it
-
Fork the project
-
Start a feature/bugfix branch
-
Commit and push until you are happy with your contribution
-
Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
Copyright © 2011 Abner Qian. See LICENSE.txt for further details.