Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Edit:</p> <p>I keep coming back to this code to use it in projects I'm in. Here's the latest which is useful for deeply nested structures and based on Pete's code above. I usually drop it in config/initializers/core_ext.rb (in a Rails project):</p> <pre><code>class Hash def deep_diff(other) (self.keys + other.keys).uniq.inject({}) do |memo, key| left = self[key] right = other[key] next memo if left == right if left.respond_to?(:deep_diff) &amp;&amp; right.respond_to?(:deep_diff) memo[key] = left.deep_diff(right) else memo[key] = [left, right] end memo end end end class Array def deep_diff(array) largest = [self.count, array.count].max memo = {} 0.upto(largest - 1) do |index| left = self[index] right = array[index] next if left == right if left.respond_to?(:deep_diff) &amp;&amp; right.respond_to?(:deep_diff) memo[index] = left.deep_diff(right) else memo[index] = [left, right] end end memo end end </code></pre> <p>Here's a small demo:</p> <pre><code>&gt; {a: [{b: "c", d: "e"}, {b: "c", f: "g"}]}.deep_diff({a: [{b: "c", d: "e"}, {b: "d", f: "g"}]}) =&gt; {:a=&gt;{1=&gt;{:b=&gt;["c", "d"]}}} </code></pre> <p>Older response:</p> <p>I have found Rails' Hash diff method to not actually tell me what was on the left side and right side (which is far more useful). There was a plugin call "Riff", that has since disappeared, which would let you diff two ActiveRecord objects. Essentially:</p> <pre><code>class Hash def diff(other) self.keys.inject({}) do |memo, key| unless self[key] == other[key] memo[key] = [self[key], other[key]] end memo end end end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload