I tried finding a gem that would give me a nice, simple diff between two hashes, and also report any missing keys between them. (In an effort to reduce the ridiculous number of update api calls sent out at work.)

I found a few gems that give way too complicated diffs, and they're all several hundred lines long. One of them even writes the diff out in freaking html with colors and everything. it's crazy. Several of the simpler ones don't even support nesting, and another only diffs strings. I found a few possibly-okay choices, but their output is crazy long, and they are none too short, either.

Also, only a few of them support missing keys (since hashes in Ruby return `nil` by default for non-defined keys), which would lead to false negatives.

So... I wrote my own.
It supports diffing anything with anything else, and recurses into anything enumerable. It also supports missing keys/indexes, mixed n-level nesting, missing branches, nil vs "nil" with obvious output, comparing mixed types, empty objects, etc. Returns a simple [a,b] diff array for simple objects, or for nested objects: a flat hash with full paths (like "[key][subkey][12][sub-subkey]") as top-level keys and the diff arrays as values. Tiny output. Took 36 lines and a little over an hour.

I'm pretty happy with myself. 😁

  • 4
    Wow🤯 can we See this code or is it confidential?

    I don't have any ruby projects but to understand how to write something like that
  • 0
    Sounds very cool, definately something to be proud of 😁

    What is the maximum allowed file size for both diffs combined?
  • 3
    I haven't a clue what I just read but:
    Mind. Blown.

    Root: "I'll do it myself!"
  • 0
    it's a pity that I never used ruby did not come across this(
  • 5
    Publish the gem on rubygems!
  • 5
    @NeatNerdPrime I just might.
Add Comment