Index: lib/set.rb =================================================================== --- lib/set.rb (revision 54238) +++ lib/set.rb (working copy) @@ -696,6 +696,25 @@ end end +# IdentitySet implements a Set that compares members based on their identity +# instead of their equality. +# +# == Example +# +# require "set" +# +# a_str = "a" +# set = IdentitySet.new([a_str, a_str, "b", "b"]) +# +# p set # => # +# +class IdentitySet < Set + def initialize(*args, &block) + @hash = Hash.new.compare_by_identity + super + end +end + module Enumerable # Makes a set from the enumerable object with given arguments. # Needs to +require "set"+ to use this method. Index: test/test_set.rb =================================================================== --- test/test_set.rb (revision 54238) +++ test/test_set.rb (working copy) @@ -710,6 +710,16 @@ end end +class TC_IdentitySet < Test::Unit::TestCase + def test_identityset + a = 'a' + s = IdentitySet[a, a, 'b', 'b'] + + assert_equal(3, s.size) + assert_equal([a, 'b', 'b'], s.to_a) + end +end + class TC_Enumerable < Test::Unit::TestCase def test_to_set ary = [2,5,4,3,2,1,3]