[前][次][番号順一覧][スレッド一覧]

ruby-changes:66966

From: Jeremy <ko1@a...>
Date: Thu, 29 Jul 2021 14:50:33 +0900 (JST)
Subject: [ruby-changes:66966] 571dafdc7f (master): [ruby/set] Allow Set#intersect? and #disjoint? to accept array argument

https://git.ruby-lang.org/ruby.git/commit/?id=571dafdc7f

From 571dafdc7f57af067706fbc318a64778f4fc218a Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Tue, 15 Jun 2021 11:08:45 -0700
Subject: [ruby/set] Allow Set#intersect? and #disjoint? to accept array
 argument

Implements [Feature #17838]

https://github.com/ruby/set/commit/d9b389bafa
---
 lib/set.rb       | 9 ++++++++-
 test/test_set.rb | 6 +++++-
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/lib/set.rb b/lib/set.rb
index 682cb74..ec4dabd 100644
--- a/lib/set.rb
+++ b/lib/set.rb
@@ -474,7 +474,14 @@ class Set https://github.com/ruby/ruby/blob/trunk/lib/set.rb#L474
   #     Set[1, 2, 3].intersect? Set[4, 5]   #=> false
   #     Set[1, 2, 3].intersect? Set[3, 4]   #=> true
   def intersect?(set)
-    set.is_a?(Set) or raise ArgumentError, "value must be a set"
+    case set
+    when Set
+      # nothing
+    when Array
+      Set.new(set)
+    else
+      raise ArgumentError, "value must be a set or array"
+    end
     if size < set.size
       any? { |o| set.include?(o) }
     else
diff --git a/test/test_set.rb b/test/test_set.rb
index af9806e..b92930a 100644
--- a/test/test_set.rb
+++ b/test/test_set.rb
@@ -354,13 +354,17 @@ class TC_Set < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_set.rb#L354
     case expected
     when true
       assert_send([set, :intersect?, other])
+      assert_send([set, :intersect?, other.to_a])
       assert_send([other, :intersect?, set])
       assert_not_send([set, :disjoint?, other])
+      assert_not_send([set, :disjoint?, other.to_a])
       assert_not_send([other, :disjoint?, set])
     when false
       assert_not_send([set, :intersect?, other])
+      assert_not_send([set, :intersect?, other.to_a])
       assert_not_send([other, :intersect?, set])
       assert_send([set, :disjoint?, other])
+      assert_send([set, :disjoint?, other.to_a])
       assert_send([other, :disjoint?, set])
     when Class
       assert_raise(expected) {
@@ -378,7 +382,7 @@ class TC_Set < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_set.rb#L382
     set = Set[3,4,5]
 
     assert_intersect(ArgumentError, set, 3)
-    assert_intersect(ArgumentError, set, [2,4,6])
+    assert_intersect(true, set, Set[2,4,6])
 
     assert_intersect(true, set, set)
     assert_intersect(true, set, Set[2,4])
-- 
cgit v1.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]