New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

hash_diff

Package Overview
Dependencies
Maintainers
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hash_diff

  • 1.1.1
  • Rubygems
  • Socket score

Version published
Maintainers
3
Created
Source

HashDiff

Ruby Code Climate Gem Version

Deep comparison of Ruby Hash objects

Installation

Add this line to your application's Gemfile:

gem 'hash_diff'

And then execute:

$ bundle

Or install it yourself as:

$ gem install hash_diff

Usage

Easily find the differences between two Ruby hashes.

  left = {
    foo: 'bar',
    bar: 'foo',
    nested: {
      foo: 'bar',
      bar: {
        one: 'foo1'
      }
    },
    num: 1,
    favorite_restaurant: "Shoney's"
  }

  right = {
    foo: 'bar2',
    bar: 'foo2',
    nested: {
      foo: 'bar2',
      bar: {
        two: 'foo2'
      }
    },
    word: 'monkey',
    favorite_restaurant: nil
  }

  hash_diff = HashDiff::Comparison.new( left, right )

Comparison#diff returns the left and right side differences

  hash_diff.diff # => { foo: ["bar", "bar2"], bar: ["foo", "foo2"], nested: { foo: ["bar", "bar2"], bar: { one: ["foo1", HashDiff::NO_VALUE], two: [HashDiff::NO_VALUE, "foo2"] } }, num:  [1, HashDiff::NO_VALUE], word: [HashDiff::NO_VALUE, "monkey"], favorite_restaurant: ["Shoney's", nil] }

You can also compare two arrays. The comparison is sensitive to the order of the elements in the array.

Missing keys

When there is a key that exists on one side it will return HashDiff::NO_VALUE to represent a missing key.

Comparison#left_diff returns only the left side differences

  hash_diff.left_diff # => {:foo=>"bar2", :bar=>"foo2", :nested=>{:foo=>"bar2", :bar=>{:one=>HashDiff::NO_VALUE, :two=>"foo2"}}, :num=>HashDiff::NO_VALUE, :favorite_restaurant=>nil, :word=>"monkey"}

Comparison#right_diff returns only the right side differences

  hash_diff.right_diff # => {:foo=>"bar", :bar=>"foo", :nested=>{:foo=>"bar", :bar=>{:one=>"foo1", :two=>HashDiff::NO_VALUE}}, :num=>1, :favorite_restaurant=>"Shoney's", :word=>HashDiff::NO_VALUE}

You can also use these shorthand methods

  HashDiff.diff(left, right)
  HashDiff.left_diff(left, right)
  HashDiff.right_diff(left, right)

Hash#diff is not provided by default, and monkey patching is frowned upon by some, but to provide a one way shorthand call HashDiff.patch!

  # run prior to implementation
  HashDiff.patch!

  left  = { foo: 'bar', num: 1 }
  right = { foo: 'baz', num: 1 }

  left.diff(right) # => { foo: 'baz' }

License

Authored by the Engineering Team of Coding ZEAL

Copyright (c) 2017 Zeal, LLC. Licensed under the MIT license.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

FAQs

Package last updated on 29 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc