🔩 Compare (consimilo) primitives such as Hash and Array deeply
Add this line to your application's Gemfile:
gem 'consimilo'
And then execute:
$ bundle
Or install it yourself as:
$ gem install consimilo
When you're comparing two states, two data sets or something similar, you might to know what has changed.
Running the two versions through Consimilo
gives a snapshot of the difference between the two, with each
difference represented as a pair [from, to]
.
require 'consimilo'
before = [1, 2 ,3]
after = [3, 2, 1]
Consimilo.difference(before, after)
# => [[1, 3], [], [3,1]]
before = { foo: [:a, :b], bar: 1, same: 0 }
after = { foo: [:a, :c], next: 3, same: 0 }
Consimilo.difference(before, after)
# => { foo: [[], [:b, :c]], bar: [1, nil], next: [nil, 3] }
By default, Consimilo
enforces Array
order. This can be turned off with the order
option:
before = [1, 2 ,3]
after = [3, 2, 1]
Consimilo.difference(before, after, order: false)
# => []
By default Consimilo
returns empty "differences" for arrays, unless the entire array is equal, so that
you may trace back at which index something has changed. This can be turned off by setting the compact
option:
before = [1, 2 ,3]
after = [3, 2, 1]
Consimilo.difference(before, after, compact: true)
# => [[1, 3], [3,1]]
You can also require 'consimilo/kernel'
in your code or Gemfile in order to enable an additional Kernel
method Consimilo(left, right, order: true, compact: false)
:
require 'consimilo/kernel'
before = [1, 2 ,3]
after = [3, 2, 1]
Consimilo(before, after, compact: true)
# => [[1, 3], [3,1]]
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
to run the tests. You can
also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the
version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version,
push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at XPBytes/consimilo.