Thursday, November 08, 2007

Rails: Hash#diff

Documentation
None

Usage
The Hash#diff method is helpful for determining the difference between two hashes.

note: The receiver matches on both keys and values. If the pair is not matched it be returned. (for example: {:a => 1, :b => 3}.diff({:a=>2}) # => {:a=>1, :b=>3})

Test

require 'rubygems'
require 'active_support'
require 'test/unit'
require 'dust'

unit_tests do
test "find the difference between two hashes" do
assert_equal({:b=>3}, {:a => 1, :b => 3}.diff({:a=>1}))
end
end

2 comments:

  1. The behavior with disjunct hashes deserves a test, too.

    test "find the difference between two disjunct hashes" do
    assert_equal({:a => 1, :x => 100}, {:a => 1}.diff({:x => 100}))
    end

    ReplyDelete
  2. Yep, definitely handy for testing.

    ReplyDelete

Note: Only a member of this blog may post a comment.