Skip to content
🔩 Compare (consimilo) primitives such as Hash and Array deeply
Ruby Shell
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.idea
bin
lib
test
.gitignore
.travis.yml
CODE_OF_CONDUCT.md
Gemfile
Gemfile.lock
LICENSE.txt
README.md
Rakefile
consimilo.gemspec

README.md

Consimilo

Build Status: master Gem Version MIT license

🔩 Compare (consimilo) primitives such as Hash and Array deeply

Installation

Add this line to your application's Gemfile:

gem 'consimilo'

And then execute:

$ bundle

Or install it yourself as:

$ gem install consimilo

Usage

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]]

Development

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.

Contributing

Bug reports and pull requests are welcome on GitHub at XPBytes/consimilo.

You can’t perform that action at this time.