Ruby on Rails
PhpArray_diff_assoc

Not sure what’s the best way to implement this because it deals with mixed Array/Hashes from PHP.

I was thinking I could just use Hash#- for this, but Hash doesn’t seem to have a – function. So here’s a reasonable implementation.


hash1 = {0=>"red", "a"=>"green", "b"=>"brown", "c"=>"blue"}
hash2 = {0=>"yellow", "a"=>"green", 1=>"red"}
p Hash[*(hash1.to_a - hash2.to_a).flatten]

Basically, we convert the hashes to array’s, use Array#- and convert back to a hash.

Note: This will break for doubly-associative arrays because the flatten method is recursive. I haven’t been able to find a single-level flatten function yet.

Hash#-


class Hash
  def -(hash2)
    Hash[*(self.to_a - hash2.to_a).flatten]
  end
end