Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added <, >, <= and >= operators to Set #59

Closed
wants to merge 1 commit into from

4 participants

Alexander E. Fischer Ayumu AIZAWA Akinori MUSHA Urabe, Shyouhei
Alexander E. Fischer

They simply serve as alternatives to the superset? and subset? method family.

Neither the Comparable mixin nor <=> is implemented by intention, because a comparison with a completely disjunct other set would result in an undefined result, which then would cause exceptions when sorting an array of sets for example.

I've also written a patch for rubyspec which tests the new methods and runs at least in MRI 1.8.7, 1.9.2 and 1.9.3. It is available here: https://github.com/aef/rubyspec

Ayumu AIZAWA
Collaborator

If you really want to let ruby-core accept patch, you should put this onto bugs.ruby-lang.org.
This opinion is new feature, so it probably doesn't be accepted in 2.0.
But it's possible on version if you got success matz's acceptance.

Good luck.

Akinori MUSHA
Collaborator

Just committed an equivalent patch, thanks.

Akinori MUSHA
Collaborator

Someone please close this, or can I be part of @ruby?

Urabe, Shyouhei
Owner

or can I be part of @ruby?

Welcome, now you are. Please close this yourself.

Akinori MUSHA
Collaborator

Merged in r36853. (Since I already had an equivalent patch I didn't pull your commit)

Akinori MUSHA knu closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 17, 2011
  1. Alexander E. Fischer
This page is out of date. Refresh to see the latest.
Showing with 13 additions and 0 deletions.
  1. +13 0 lib/set.rb
13 lib/set.rb
View
@@ -36,6 +36,15 @@
# Enumerable objects besides sets and arrays. An Enumerable object
# can be converted to Set using the +to_set+ method.
#
+# == Comparison
+#
+# Although <, >, <= and >= are implemented as alternative for the
+# superset? and subset? method family neither the Comparable mixin
+# nor <=> is implemented by intention, because a comparison with a
+# completely disjunct other set would result in an undefined
+# result, which then would cause exceptions when sorting an array of
+# sets for example.
+#
# == Example
#
# require 'set'
@@ -192,6 +201,7 @@ def superset?(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 +209,7 @@ def proper_superset?(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 +217,7 @@ def subset?(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 +225,7 @@ def proper_subset?(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
Something went wrong with that request. Please try again.