Class: ImmutableSet
- Inherits:
-
Object
- Object
- ImmutableSet
- Defined in:
- lib/values/immutable_set.rb
Defined Under Namespace
Classes: SetChange
Instance Method Summary collapse
- #==(other) ⇒ Object
- #add(*values) ⇒ Object
- #complement(other_set) ⇒ Object
- #delete(*values) ⇒ Object (also: #remove)
- #each(&block) ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
- #include?(value) ⇒ Boolean
-
#initialize(*args) ⇒ ImmutableSet
constructor
A new instance of ImmutableSet.
- #intersect(other_set) ⇒ Object
- #proper_subset?(other_set) ⇒ Boolean (also: #proper_subset_of?)
- #proper_superset?(other_set) ⇒ Boolean (also: #proper_superset_of?)
- #size ⇒ Object (also: #count)
- #subset?(other_set) ⇒ Boolean
- #superset?(other_set) ⇒ Boolean (also: #superset_of?)
- #to_a ⇒ Object
- #union(*values) ⇒ Object
Constructor Details
#initialize(*args) ⇒ ImmutableSet
Returns a new instance of ImmutableSet.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/values/immutable_set.rb', line 6 def initialize(*args) if args.first.is_a? ImmutableSet::SetChange @set_change = args.first else @set = Set.new; args.each do |value| if value.is_a? ImmutableSet @set.merge value.set elsif value.is_a? Set @set.merge value elsif value.is_a? Array @set.merge value elsif not value.nil? @set.add value end end end end |
Instance Method Details
#==(other) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/values/immutable_set.rb', line 52 def == other if other.equal?(self) true elsif !self.class.equal?(other.class) false else self.set.eql?(other.set) end end |
#add(*values) ⇒ Object
40 41 42 |
# File 'lib/values/immutable_set.rb', line 40 def add *values ImmutableSet.new(SetChange.new(values,@set || @set_change.set,true,@set_change)) end |
#complement(other_set) ⇒ Object
108 109 110 |
# File 'lib/values/immutable_set.rb', line 108 def complement other_set ImmutableSet.new(self.set - other_set.set) end |
#delete(*values) ⇒ Object Also known as: remove
62 63 64 |
# File 'lib/values/immutable_set.rb', line 62 def delete *values ImmutableSet.new(SetChange.new(values,@set || @set_change.set,false,@set_change)) end |
#each(&block) ⇒ Object
34 35 36 37 38 |
# File 'lib/values/immutable_set.rb', line 34 def each &block self.set.to_a.each do |value| block.call value end end |
#eql?(other) ⇒ Boolean
48 49 50 |
# File 'lib/values/immutable_set.rb', line 48 def eql? other self == other end |
#hash ⇒ Object
44 45 46 |
# File 'lib/values/immutable_set.rb', line 44 def hash self.set.hash end |
#include?(value) ⇒ Boolean
72 73 74 |
# File 'lib/values/immutable_set.rb', line 72 def include? value self.set.include? value end |
#intersect(other_set) ⇒ Object
112 113 114 |
# File 'lib/values/immutable_set.rb', line 112 def intersect other_set ImmutableSet.new(self.set & other_set.set) end |
#proper_subset?(other_set) ⇒ Boolean Also known as: proper_subset_of?
92 93 94 |
# File 'lib/values/immutable_set.rb', line 92 def proper_subset? other_set self.set.proper_subset?(other_set.set) end |
#proper_superset?(other_set) ⇒ Boolean Also known as: proper_superset_of?
98 99 100 |
# File 'lib/values/immutable_set.rb', line 98 def proper_superset? other_set self.set.proper_superset?(other_set.set) end |
#size ⇒ Object Also known as: count
28 29 30 |
# File 'lib/values/immutable_set.rb', line 28 def size self.set.size end |
#subset?(other_set) ⇒ Boolean
82 83 84 |
# File 'lib/values/immutable_set.rb', line 82 def subset? other_set self.set.subset?(other_set.set) end |
#superset?(other_set) ⇒ Boolean Also known as: superset_of?
86 87 88 |
# File 'lib/values/immutable_set.rb', line 86 def superset? other_set self.set.superset?(other_set.set) end |
#to_a ⇒ Object
68 69 70 |
# File 'lib/values/immutable_set.rb', line 68 def to_a self.set.to_a end |
#union(*values) ⇒ Object
104 105 106 |
# File 'lib/values/immutable_set.rb', line 104 def union *values self.add *values end |