From 1958afd2144d24955646874d82f253a989f0e3ce Mon Sep 17 00:00:00 2001 From: pmcnano Date: Fri, 19 Nov 2021 10:14:13 +0000 Subject: [PATCH] [Fix] SortedSet#include? returns false For the Set gem, `@hash = Hash.new(false)`, for the SortedSet it's initialized with a `RBTree` with no default value, which causes the include method to return `nil` when the key is not present. Fixing it and adding a couple of specs. --- lib/sorted_set.rb | 2 +- test/test_sorted_set.rb | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/sorted_set.rb b/lib/sorted_set.rb index 305f60b..7ca5fa7 100644 --- a/lib/sorted_set.rb +++ b/lib/sorted_set.rb @@ -51,7 +51,7 @@ class SortedSet < Set # Creates a SortedSet. See Set.new for details. def initialize(*args) - @hash = RBTree.new + @hash = RBTree.new(false) super end end diff --git a/test/test_sorted_set.rb b/test/test_sorted_set.rb index 3b7622a..6b091e2 100644 --- a/test/test_sorted_set.rb +++ b/test/test_sorted_set.rb @@ -46,6 +46,10 @@ def test_sortedset assert_same(nil, ret) assert_equal(['four', 'one', 'three', 'two'], s.to_a) assert_equal(['four', 'one', 'three', 'two'], a) + + s = SortedSet.new([1,2,3]) + assert_equal(true, s.include?(1)) + assert_equal(false, s.include?(5)) end def test_each