ruby-changes:47846
From: knu <ko1@a...>
Date: Tue, 19 Sep 2017 17:45:16 +0900 (JST)
Subject: [ruby-changes:47846] knu:r59966 (trunk): Alias Set#=== to #include?
knu 2017-09-19 17:45:12 +0900 (Tue, 19 Sep 2017) New Revision: 59966 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59966 Log: Alias Set#=== to #include? * set.rb (Set#===): Added via [Feature #13801] by davidarnold. Closes #1673. Added files: trunk/spec/rubyspec/library/set/case_equality_spec.rb trunk/spec/rubyspec/library/set/sortedset/case_equality_spec.rb Modified files: trunk/NEWS trunk/lib/set.rb trunk/test/test_set.rb Index: spec/rubyspec/library/set/sortedset/case_equality_spec.rb =================================================================== --- spec/rubyspec/library/set/sortedset/case_equality_spec.rb (nonexistent) +++ spec/rubyspec/library/set/sortedset/case_equality_spec.rb (revision 59966) @@ -0,0 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/spec/rubyspec/library/set/sortedset/case_equality_spec.rb#L1 +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 Property changes on: spec/rubyspec/library/set/sortedset/case_equality_spec.rb ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +LF \ No newline at end of property Index: spec/rubyspec/library/set/case_equality_spec.rb =================================================================== --- spec/rubyspec/library/set/case_equality_spec.rb (nonexistent) +++ spec/rubyspec/library/set/case_equality_spec.rb (revision 59966) @@ -0,0 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/spec/rubyspec/library/set/case_equality_spec.rb#L1 +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 Property changes on: spec/rubyspec/library/set/case_equality_spec.rb ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +LF \ No newline at end of property Index: NEWS =================================================================== --- NEWS (revision 59965) +++ NEWS (revision 59966) @@ -149,6 +149,7 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L149 * Set * Add Set#to_s as alias to #inspect [Feature #13676] + * Add Set#=== as alias to #inspect [Feature #13801] * WEBrick Index: lib/set.rb =================================================================== --- lib/set.rb (revision 59965) +++ lib/set.rb (revision 59966) @@ -475,6 +475,23 @@ class Set https://github.com/ruby/ruby/blob/trunk/lib/set.rb#L475 @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 Index: test/test_set.rb =================================================================== --- test/test_set.rb (revision 59965) +++ test/test_set.rb (revision 59966) @@ -200,6 +200,23 @@ class TC_Set < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_set.rb#L200 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] -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/