Project

General

Profile

Feature #12210 ยป identity_set.diff

patch - tjwp (Tim Perkins), 03/23/2016 07:20 PM

View differences:

lib/set.rb (working copy)
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 # => #<IdentitySet: {"a", "b", "b"}>
#
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.
test/test_set.rb (working copy)
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]
    (1-1/1)