Skip to content

Instantly share code, notes, and snippets.

@fronx
Created October 28, 2009 20:14
Show Gist options
  • Select an option

  • Save fronx/220782 to your computer and use it in GitHub Desktop.

Select an option

Save fronx/220782 to your computer and use it in GitHub Desktop.
difference between two hashes
class Hash
def -(h)
reject do |k, v|
h.has_key?(k) && (v == h[k])
end
end
end
describe Hash, 'difference between two hashes' do
it "should return a hash" do
({:a => 1, :b => 2} - {:a => 1}).should == {:b => 2}
({:a => 2, :b => 2} - {:a => 1}).should == {:b => 2, :a => 2}
({:a => 1, :b => 2} - {:c => 1}).should == {:a => 1, :b => 2}
({:a => ''} - {:a => 'not empty'}).should == {:a => ''}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment