From e2faa6a0e5df2f0878ebaf2e7b34fc2e21db04c6 Mon Sep 17 00:00:00 2001 From: mrhoribu <15917743+mrhoribu@users.noreply.github.com> Date: Sat, 24 Oct 2026 11:25:49 -0400 Subject: [PATCH] Set operations retain compare_by_identity flag Set#&, #classify, #divide, #flatten, #flatten! and #map! keep the receiver's compare_by_identity flag since the fix for [Bug #22174]. See https://bugs.ruby-lang.org/issues/22174 Same as the specs from ruby/ruby#18848. Co-Authored-By: Claude Opus 5.5 --- core/set/classify_spec.rb | 18 ++++++++++++++---- core/set/divide_spec.rb | 18 ++++++++++++++---- core/set/flatten_spec.rb | 33 +++++++++++++++++++++++++-------- core/set/intersection_spec.rb | 18 ++++++++++++++---- core/set/map_spec.rb | 18 ++++++++++++++---- 5 files changed, 81 insertions(+), 24 deletions(-) diff --git a/core/set/classify_spec.rb b/core/set/classify_spec.rb index 1cfacf357..a9521d3b5 100644 --- a/core/set/classify_spec.rb +++ b/core/set/classify_spec.rb @@ -24,9 +24,19 @@ classified.should == { 3 => Set["one", "two"], 4 => Set["four"], 5 => Set["three"] } end - it "does not retain compare_by_identity flag" do - set = Set["one", "two"].compare_by_identity - classified = set.classify { |x| x.length } - classified.values.each { |s| s.compare_by_identity?.should == false } + ruby_version_is "4.1" do + it "retains compare_by_identity flag" do + set = Set["one", "two"].compare_by_identity + classified = set.classify { |x| x.length } + classified.values.each { |s| s.compare_by_identity?.should == true } + end + end + + ruby_version_is ""..."4.0" do + it "does not retain compare_by_identity flag" do + set = Set["one", "two"].compare_by_identity + classified = set.classify { |x| x.length } + classified.values.each { |s| s.compare_by_identity?.should == false } + end end end diff --git a/core/set/divide_spec.rb b/core/set/divide_spec.rb index e00e4616e..96880db9b 100644 --- a/core/set/divide_spec.rb +++ b/core/set/divide_spec.rb @@ -18,10 +18,20 @@ ret.each(&:even?).should == Set[Set[1, 3], Set[2, 4]] end - it "does not retain compare_by_identity flag" do - set = Set["one", "two"].compare_by_identity - res = set.divide { |x| x.length } - res.each { |s| s.compare_by_identity?.should == false } + ruby_version_is "4.1" do + it "retains compare_by_identity flag" do + set = Set["one", "two"].compare_by_identity + res = set.divide { |x| x.length } + res.each { |s| s.compare_by_identity?.should == true } + end + end + + ruby_version_is ""..."4.0" do + it "does not retain compare_by_identity flag" do + set = Set["one", "two"].compare_by_identity + res = set.divide { |x| x.length } + res.each { |s| s.compare_by_identity?.should == false } + end end end diff --git a/core/set/flatten_spec.rb b/core/set/flatten_spec.rb index 65c63a9b7..9fc05ea37 100644 --- a/core/set/flatten_spec.rb +++ b/core/set/flatten_spec.rb @@ -16,17 +16,24 @@ -> { set.flatten }.should.raise(ArgumentError) end + ruby_version_is "4.1" do + it "retains compare_by_identity flag" do + set = Set[1, 2, Set[3, 4]].compare_by_identity + set.flatten.compare_by_identity?.should == true + end + end + ruby_version_is ""..."4.0" do context "when Set contains a Set-like object" do it "returns a copy of self with each included Set-like object flattened" do Set[SetSpecs::SetLike.new([1])].flatten.should == Set[1] end end - end - it "does not retain compare_by_identity flag" do - set = Set[1, 2, Set[3, 4]].compare_by_identity - set.flatten.compare_by_identity?.should == false + it "does not retain compare_by_identity flag" do + set = Set[1, 2, Set[3, 4]].compare_by_identity + set.flatten.compare_by_identity?.should == false + end end end @@ -52,10 +59,20 @@ -> { set.flatten! }.should.raise(ArgumentError) end - it "does not retain compare_by_identity flag when flattening elements" do - set = Set[1, 2, Set[3, 4]].compare_by_identity - set.flatten! - set.compare_by_identity?.should == false + ruby_version_is "4.1" do + it "retains compare_by_identity flag when flattening elements" do + set = Set[1, 2, Set[3, 4]].compare_by_identity + set.flatten! + set.compare_by_identity?.should == true + end + end + + ruby_version_is ""..."4.0" do + it "does not retain compare_by_identity flag when flattening elements" do + set = Set[1, 2, Set[3, 4]].compare_by_identity + set.flatten! + set.compare_by_identity?.should == false + end end it "retains compare_by_identity flag if no elements are flattened" do diff --git a/core/set/intersection_spec.rb b/core/set/intersection_spec.rb index 3551fe701..2e1388e0c 100644 --- a/core/set/intersection_spec.rb +++ b/core/set/intersection_spec.rb @@ -21,9 +21,19 @@ -> { @set & Object.new }.should.raise(ArgumentError) end - it "does not retain compare_by_identity flag" do - @set.compare_by_identity - (@set & Set[:b, :c, :d, :e]).compare_by_identity?.should == false - (@set & [:b, :c, :d]).compare_by_identity?.should == false + ruby_version_is "4.1" do + it "retains compare_by_identity flag" do + @set.compare_by_identity + (@set & Set[:b, :c, :d, :e]).compare_by_identity?.should == true + (@set & [:b, :c, :d]).compare_by_identity?.should == true + end + end + + ruby_version_is ""..."4.0" do + it "does not retain compare_by_identity flag" do + @set.compare_by_identity + (@set & Set[:b, :c, :d, :e]).compare_by_identity?.should == false + (@set & [:b, :c, :d]).compare_by_identity?.should == false + end end end diff --git a/core/set/map_spec.rb b/core/set/map_spec.rb index 6f6959fee..7121afc45 100644 --- a/core/set/map_spec.rb +++ b/core/set/map_spec.rb @@ -20,9 +20,19 @@ @set.should == Set[2, 4, 6, 8, 10] end - it "does not retain compare_by_identity flag" do - @set.compare_by_identity - @set.map! { |x| x * 2 } - @set.compare_by_identity?.should == false + ruby_version_is "4.1" do + it "retains compare_by_identity flag" do + @set.compare_by_identity + @set.map! { |x| x * 2 } + @set.compare_by_identity?.should == true + end + end + + ruby_version_is ""..."4.0" do + it "does not retain compare_by_identity flag" do + @set.compare_by_identity + @set.map! { |x| x * 2 } + @set.compare_by_identity?.should == false + end end end