Created
October 28, 2009 20:14
-
-
Save fronx/220782 to your computer and use it in GitHub Desktop.
difference between two hashes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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