summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-19 08:45:12 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-19 08:45:12 +0000
commit2aee703e7a29bcfc2095e40870f5a3c45cd96dd2 (patch)
tree3ea77cd20f5e90e98356845e8dbda006bc8d9f48
parentb40a9475db568d05bf33ab168ed804e3e2091293 (diff)
Alias Set#=== to #include?
* set.rb (Set#===): Added via [Feature #13801] by davidarnold. Closes #1673. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59966 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--NEWS1
-rw-r--r--lib/set.rb17
-rw-r--r--spec/rubyspec/library/set/case_equality_spec.rb7
-rw-r--r--spec/rubyspec/library/set/sortedset/case_equality_spec.rb7
-rw-r--r--test/test_set.rb17
5 files changed, 49 insertions, 0 deletions
diff --git a/NEWS b/NEWS
index 717ae69b65..940491ae39 100644
--- a/NEWS
+++ b/NEWS
@@ -149,6 +149,7 @@ with all sufficient information, see the ChangeLog file or Redmine
* Set
* Add Set#to_s as alias to #inspect [Feature #13676]
+ * Add Set#=== as alias to #inspect [Feature #13801]
* WEBrick
diff --git a/lib/set.rb b/lib/set.rb
index bfa37d4dc0..009721381b 100644
--- a/lib/set.rb
+++ b/lib/set.rb
@@ -475,6 +475,23 @@ class Set
@hash.eql?(o.instance_variable_get(:@hash))
end
+ # Returns true if obj is a member of the set, and false otherwise.
+ #
+ # Used in case statements:
+ #
+ # case :apple
+ # when Set[:potato, :carrot] then 'vegetable'
+ # when Set[:apple, :banana] then 'fruit'
+ # end
+ # #=> "fruit"
+ #
+ # Or by itself:
+ #
+ # Set[1, 2, 3] === 2 #=> true
+ # Set[1, 2, 3] === 4 #=> false
+ #
+ alias === include?
+
# Classifies the set by the return value of the given block and
# returns a hash of {value => set of elements} pairs. The block is
# called once for each element of the set, passing the element as
diff --git a/spec/rubyspec/library/set/case_equality_spec.rb b/spec/rubyspec/library/set/case_equality_spec.rb
new file mode 100644
index 0000000000..ca1e900557
--- /dev/null
+++ b/spec/rubyspec/library/set/case_equality_spec.rb
@@ -0,0 +1,7 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/include', __FILE__)
+require 'set'
+
+describe "Set#===" do
+ it_behaves_like :set_include, :===
+end
diff --git a/spec/rubyspec/library/set/sortedset/case_equality_spec.rb b/spec/rubyspec/library/set/sortedset/case_equality_spec.rb
new file mode 100644
index 0000000000..f5ff2c91fe
--- /dev/null
+++ b/spec/rubyspec/library/set/sortedset/case_equality_spec.rb
@@ -0,0 +1,7 @@
+require File.expand_path('../../../../spec_helper', __FILE__)
+require File.expand_path('../shared/include', __FILE__)
+require 'set'
+
+describe "SortedSet#===" do
+ it_behaves_like :sorted_set_include, :===
+end
diff --git a/test/test_set.rb b/test/test_set.rb
index aaf3bfe3b8..daae12723d 100644
--- a/test/test_set.rb
+++ b/test/test_set.rb
@@ -200,6 +200,23 @@ class TC_Set < Test::Unit::TestCase
assert_equal(false, set.include?(true))
end
+ def test_eqq
+ set = Set[1,2,3]
+
+ assert_equal(true, set === 1)
+ assert_equal(true, set === 2)
+ assert_equal(true, set === 3)
+ assert_equal(false, set === 0)
+ assert_equal(false, set === nil)
+
+ set = Set["1",nil,"2",nil,"0","1",false]
+ assert_equal(true, set === nil)
+ assert_equal(true, set === false)
+ assert_equal(true, set === "1")
+ assert_equal(false, set === 0)
+ assert_equal(false, set === true)
+ end
+
def test_superset?
set = Set[1,2,3]