mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
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
This commit is contained in:
parent
b40a9475db
commit
2aee703e7a
5 changed files with 49 additions and 0 deletions
1
NEWS
1
NEWS
|
@ -149,6 +149,7 @@ with all sufficient information, see the ChangeLog file or Redmine
|
||||||
|
|
||||||
* Set
|
* Set
|
||||||
* Add Set#to_s as alias to #inspect [Feature #13676]
|
* Add Set#to_s as alias to #inspect [Feature #13676]
|
||||||
|
* Add Set#=== as alias to #inspect [Feature #13801]
|
||||||
|
|
||||||
* WEBrick
|
* WEBrick
|
||||||
|
|
||||||
|
|
17
lib/set.rb
17
lib/set.rb
|
@ -475,6 +475,23 @@ class Set
|
||||||
@hash.eql?(o.instance_variable_get(:@hash))
|
@hash.eql?(o.instance_variable_get(:@hash))
|
||||||
end
|
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
|
# Classifies the set by the return value of the given block and
|
||||||
# returns a hash of {value => set of elements} pairs. The block is
|
# returns a hash of {value => set of elements} pairs. The block is
|
||||||
# called once for each element of the set, passing the element as
|
# called once for each element of the set, passing the element as
|
||||||
|
|
7
spec/rubyspec/library/set/case_equality_spec.rb
Normal file
7
spec/rubyspec/library/set/case_equality_spec.rb
Normal file
|
@ -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
|
|
@ -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
|
|
@ -200,6 +200,23 @@ class TC_Set < Test::Unit::TestCase
|
||||||
assert_equal(false, set.include?(true))
|
assert_equal(false, set.include?(true))
|
||||||
end
|
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?
|
def test_superset?
|
||||||
set = Set[1,2,3]
|
set = Set[1,2,3]
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue