diff options
-rw-r--r-- | NEWS | 1 | ||||
-rw-r--r-- | lib/set.rb | 17 | ||||
-rw-r--r-- | spec/rubyspec/library/set/case_equality_spec.rb | 7 | ||||
-rw-r--r-- | spec/rubyspec/library/set/sortedset/case_equality_spec.rb | 7 | ||||
-rw-r--r-- | test/test_set.rb | 17 |
5 files changed, 49 insertions, 0 deletions
@@ -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] |