Skip to content

Commit 75cfad3

Browse files
mrhoribuclaude
andcommitted
Set operations retain compare_by_identity flag on 4.0
Set#&, #classify, #divide, #flatten, #flatten! and #map! keep the receiver's compare_by_identity flag since the fix for [Bug #22174] was backported to Ruby 4.0 (4.0.8). See https://bugs.ruby-lang.org/issues/22174 Same as the specs from ruby/ruby#18848, with the "4.1" guards changed to "4.0" for the backport. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 417be8a commit 75cfad3

5 files changed

Lines changed: 81 additions & 24 deletions

File tree

‎core/set/classify_spec.rb‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,19 @@
2424
classified.should == { 3 => Set["one", "two"], 4 => Set["four"], 5 => Set["three"] }
2525
end
2626

27-
it "does not retain compare_by_identity flag" do
28-
set = Set["one", "two"].compare_by_identity
29-
classified = set.classify { |x| x.length }
30-
classified.values.each { |s| s.compare_by_identity?.should == false }
27+
ruby_version_is "4.0" do
28+
it "retains compare_by_identity flag" do
29+
set = Set["one", "two"].compare_by_identity
30+
classified = set.classify { |x| x.length }
31+
classified.values.each { |s| s.compare_by_identity?.should == true }
32+
end
33+
end
34+
35+
ruby_version_is ""..."4.0" do
36+
it "does not retain compare_by_identity flag" do
37+
set = Set["one", "two"].compare_by_identity
38+
classified = set.classify { |x| x.length }
39+
classified.values.each { |s| s.compare_by_identity?.should == false }
40+
end
3141
end
3242
end

‎core/set/divide_spec.rb‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,20 @@
1818
ret.each(&:even?).should == Set[Set[1, 3], Set[2, 4]]
1919
end
2020

21-
it "does not retain compare_by_identity flag" do
22-
set = Set["one", "two"].compare_by_identity
23-
res = set.divide { |x| x.length }
24-
res.each { |s| s.compare_by_identity?.should == false }
21+
ruby_version_is "4.0" do
22+
it "retains compare_by_identity flag" do
23+
set = Set["one", "two"].compare_by_identity
24+
res = set.divide { |x| x.length }
25+
res.each { |s| s.compare_by_identity?.should == true }
26+
end
27+
end
28+
29+
ruby_version_is ""..."4.0" do
30+
it "does not retain compare_by_identity flag" do
31+
set = Set["one", "two"].compare_by_identity
32+
res = set.divide { |x| x.length }
33+
res.each { |s| s.compare_by_identity?.should == false }
34+
end
2535
end
2636
end
2737

‎core/set/flatten_spec.rb‎

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,24 @@
1616
-> { set.flatten }.should.raise(ArgumentError)
1717
end
1818

19+
ruby_version_is "4.0" do
20+
it "retains compare_by_identity flag" do
21+
set = Set[1, 2, Set[3, 4]].compare_by_identity
22+
set.flatten.compare_by_identity?.should == true
23+
end
24+
end
25+
1926
ruby_version_is ""..."4.0" do
2027
context "when Set contains a Set-like object" do
2128
it "returns a copy of self with each included Set-like object flattened" do
2229
Set[SetSpecs::SetLike.new([1])].flatten.should == Set[1]
2330
end
2431
end
25-
end
2632

27-
it "does not retain compare_by_identity flag" do
28-
set = Set[1, 2, Set[3, 4]].compare_by_identity
29-
set.flatten.compare_by_identity?.should == false
33+
it "does not retain compare_by_identity flag" do
34+
set = Set[1, 2, Set[3, 4]].compare_by_identity
35+
set.flatten.compare_by_identity?.should == false
36+
end
3037
end
3138
end
3239

@@ -52,10 +59,20 @@
5259
-> { set.flatten! }.should.raise(ArgumentError)
5360
end
5461

55-
it "does not retain compare_by_identity flag when flattening elements" do
56-
set = Set[1, 2, Set[3, 4]].compare_by_identity
57-
set.flatten!
58-
set.compare_by_identity?.should == false
62+
ruby_version_is "4.0" do
63+
it "retains compare_by_identity flag when flattening elements" do
64+
set = Set[1, 2, Set[3, 4]].compare_by_identity
65+
set.flatten!
66+
set.compare_by_identity?.should == true
67+
end
68+
end
69+
70+
ruby_version_is ""..."4.0" do
71+
it "does not retain compare_by_identity flag when flattening elements" do
72+
set = Set[1, 2, Set[3, 4]].compare_by_identity
73+
set.flatten!
74+
set.compare_by_identity?.should == false
75+
end
5976
end
6077

6178
it "retains compare_by_identity flag if no elements are flattened" do

‎core/set/intersection_spec.rb‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,19 @@
2121
-> { @set & Object.new }.should.raise(ArgumentError)
2222
end
2323

24-
it "does not retain compare_by_identity flag" do
25-
@set.compare_by_identity
26-
(@set & Set[:b, :c, :d, :e]).compare_by_identity?.should == false
27-
(@set & [:b, :c, :d]).compare_by_identity?.should == false
24+
ruby_version_is "4.0" do
25+
it "retains compare_by_identity flag" do
26+
@set.compare_by_identity
27+
(@set & Set[:b, :c, :d, :e]).compare_by_identity?.should == true
28+
(@set & [:b, :c, :d]).compare_by_identity?.should == true
29+
end
30+
end
31+
32+
ruby_version_is ""..."4.0" do
33+
it "does not retain compare_by_identity flag" do
34+
@set.compare_by_identity
35+
(@set & Set[:b, :c, :d, :e]).compare_by_identity?.should == false
36+
(@set & [:b, :c, :d]).compare_by_identity?.should == false
37+
end
2838
end
2939
end

‎core/set/map_spec.rb‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,19 @@
2020
@set.should == Set[2, 4, 6, 8, 10]
2121
end
2222

23-
it "does not retain compare_by_identity flag" do
24-
@set.compare_by_identity
25-
@set.map! { |x| x * 2 }
26-
@set.compare_by_identity?.should == false
23+
ruby_version_is "4.0" do
24+
it "retains compare_by_identity flag" do
25+
@set.compare_by_identity
26+
@set.map! { |x| x * 2 }
27+
@set.compare_by_identity?.should == true
28+
end
29+
end
30+
31+
ruby_version_is ""..."4.0" do
32+
it "does not retain compare_by_identity flag" do
33+
@set.compare_by_identity
34+
@set.map! { |x| x * 2 }
35+
@set.compare_by_identity?.should == false
36+
end
2737
end
2838
end

0 commit comments

Comments
 (0)