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

Labels: ,




Comments:
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
 
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?