Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added <, >, <= and >= operators as alternative to the subset? and sup…
…erset? method family
  • Loading branch information
aef committed Nov 17, 2011
commit a775f9711e2666b9f28b5087a20fd6ab4cbf6f6f
13 changes: 13 additions & 0 deletions lib/set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -192,27 +201,31 @@ 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)
set.is_a?(Set) or raise ArgumentError, "value must be a 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)
set.is_a?(Set) or raise ArgumentError, "value must be a 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)
set.is_a?(Set) or raise ArgumentError, "value must be a 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
Expand Down