summaryrefslogtreecommitdiff
path: root/lib/set.rb
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-08-30 04:01:58 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-08-30 04:01:58 +0000
commitaa7dc0f305c11c2ef9e67f6d48acbd1264022024 (patch)
tree4ae90c77c709b337c6c37b7a0249d9ab718dc8ba /lib/set.rb
parent37cc18d54bf008cf4ab8e0c39829f96efdfe037e (diff)
* 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
Diffstat (limited to 'lib/set.rb')
-rwxr-xr-xlib/set.rb23
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/set.rb b/lib/set.rb
index d1e2a2e..9727ba3 100755
--- a/lib/set.rb
+++ b/lib/set.rb
@@ -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