* lib/set.rb (Set#{<,>,<=,>=}): Define comparison operators as

shorthand for the {proper_}{subset?,superset?} methods (finally).
  Given a push by Alexander E. Fischer.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36853 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
knu 2012-08-30 04:01:58 +00:00
Родитель 37cc18d54b
Коммит aa7dc0f305
2 изменённых файлов: 29 добавлений и 0 удалений

Просмотреть файл

@ -1,3 +1,9 @@
Thu Aug 30 12:44:43 2012 Akinori MUSHA <knu@iDaemons.org>
* lib/set.rb (Set#{<,>,<=,>=}): Define comparison operators as
shorthand for the {proper_}{subset?,superset?} methods (finally).
Given a push by Alexander E. Fischer.
Thu Aug 30 09:21:01 2012 NARUSE, Yui <naruse@ruby-lang.org>
* lib/uri/ftp.rb (URI::FTP#initialize): raise InvalidURIError if "//"

Просмотреть файл

@ -36,6 +36,13 @@
# Enumerable objects besides sets and arrays. An Enumerable object
# can be converted to Set using the +to_set+ method.
#
# == Comparison
#
# The comparison operators <, >, <= and >= are implemented as
# shorthand for the {proper_,}{subset?,superset?} methods. However,
# the <=> operator is intentionally left out because not every pair of
# sets is comparable. ({x,y} vs. {x,z} for example)
#
# == Example
#
# require 'set'
@ -192,6 +199,7 @@ class Set
return false if size < set.size
set.all? { |o| include?(o) }
end
alias >= superset?
# Returns true if the set is a proper superset of the given set.
def proper_superset?(set)
@ -199,6 +207,7 @@ class Set
return false if size <= set.size
set.all? { |o| include?(o) }
end
alias > proper_superset?
# Returns true if the set is a subset of the given set.
def subset?(set)
@ -206,6 +215,7 @@ class Set
return false if set.size < size
all? { |o| set.include?(o) }
end
alias <= subset?
# Returns true if the set is a proper subset of the given set.
def proper_subset?(set)
@ -213,6 +223,7 @@ class Set
return false if set.size <= size
all? { |o| set.include?(o) }
end
alias < proper_subset?
# Calls the given block once for each element in the set, passing
# the element as parameter. Returns an enumerator if no block is
@ -937,6 +948,9 @@ class TC_Set < Test::Unit::TestCase
assert_equal(false, set.superset?(Set[1,2,3,4]))
assert_equal(false, set.superset?(Set[1,4]))
assert_equal(true, set >= Set[1,2,3])
assert_equal(true, set >= Set[1,2])
assert_equal(true, Set[].superset?(Set[]))
end
@ -961,6 +975,9 @@ class TC_Set < Test::Unit::TestCase
assert_equal(false, set.proper_superset?(Set[1,2,3,4]))
assert_equal(false, set.proper_superset?(Set[1,4]))
assert_equal(false, set > Set[1,2,3])
assert_equal(true, set > Set[1,2])
assert_equal(false, Set[].proper_superset?(Set[]))
end
@ -984,6 +1001,9 @@ class TC_Set < Test::Unit::TestCase
assert_equal(false, set.subset?(Set[1,2]))
assert_equal(false, set.subset?(Set[]))
assert_equal(true, set <= Set[1,2,3])
assert_equal(true, set <= Set[1,2,3,4])
assert_equal(true, Set[].subset?(Set[1]))
assert_equal(true, Set[].subset?(Set[]))
end
@ -1008,6 +1028,9 @@ class TC_Set < Test::Unit::TestCase
assert_equal(false, set.proper_subset?(Set[1,2]))
assert_equal(false, set.proper_subset?(Set[]))
assert_equal(false, set < Set[1,2,3])
assert_equal(true, set < Set[1,2,3,4])
assert_equal(false, Set[].proper_subset?(Set[]))
end