Class: Sorted::Set
- Inherits:
-
Object
- Object
- Sorted::Set
- Includes:
- Comparable, Enumerable
- Defined in:
- lib/sorted/set.rb
Instance Method Summary collapse
-
#+(other_set) ⇒ Object
Concatenation - Returns a new set built by concatenating the two sets together to produce a third set.
-
#-(other_set) ⇒ Object
Array Difference - Returns a new set that is a copy of the original set, removing any items that also appear in
other_set. -
#<<(ary) ⇒ Object
Append - Pushes the given order array on to the end of this set.
-
#<=>(other_set) ⇒ Object
Comparison - Returns an integer (-1, 0, or +1) if this array is less than, equal to, or greater than
other_set. -
#assoc(item) ⇒ Object
Searches through an array whose elements are also arrays comparing
itemwith the first element of each contained array usingitem.==. -
#at(index) ⇒ Object
Returns key, order array pair at index
index. -
#direction_intersect(other_set) ⇒ Object
Returns a new array containing elements common to the two arrays, excluding any duplicates.
-
#each ⇒ Object
Calls the given block once for each element in self, passing that element as a parameter.
-
#initialize(ary = []) ⇒ Set
constructor
A new instance of Set.
-
#keys ⇒ Object
Returns the keys form the array pairs.
-
#length ⇒ Object
(also: #size)
Returns the number of elements in self.
-
#reject ⇒ Object
Returns a new set containing the items in self for which the given block is not true.
-
#select ⇒ Object
Returns a new set containing all elements of self for which the given block returns a true value.
-
#to_a ⇒ Object
Returns the underlying array for the set object.
-
#to_h ⇒ Object
Returns the result of interpreting ary as an array of [key, value] pairs.
-
#uniq ⇒ Object
Returns a new set by removing duplicate values in self.
Constructor Details
#initialize(ary = []) ⇒ Set
Returns a new instance of Set.
6 7 8 |
# File 'lib/sorted/set.rb', line 6 def initialize(ary = []) @ary = ary end |
Instance Method Details
#+(other_set) ⇒ Object
81 82 83 |
# File 'lib/sorted/set.rb', line 81 def +(other_set) self.class.new(@ary + other_set.to_a) end |
#-(other_set) ⇒ Object
Array Difference - Returns a new set that is a copy of the original set, removing any items that also appear in other_set. The order is preserved from the original set.
set = Sorted::Set.new(['email', 'desc'])
other_set = Sorted::Set.new(['phone', 'asc'])
set - other_set #=> #<Sorted::Set:0x007fafde1ead80>
63 64 65 66 67 68 69 70 71 |
# File 'lib/sorted/set.rb', line 63 def -(other_set) self.class.new.tap do |memo| each do |a| b = other_set.assoc(a.first) next if b memo << a end end end |
#<<(ary) ⇒ Object
Append - Pushes the given order array on to the end of this set. This expression returns the set itself, so several appends may be chained together.
set = Sorted::Set.new(['name', 'asc'])
set << ['email', 'desc'] << ['phone', 'asc']
set.to_a #=> [['name', 'asc'], ['email', 'desc'], ['phone', 'asc']]
94 95 96 97 |
# File 'lib/sorted/set.rb', line 94 def <<(ary) @ary << ary self end |
#<=>(other_set) ⇒ Object
Comparison - Returns an integer (-1, 0, or +1) if this array is less than, equal to, or greater than other_set.
125 126 127 |
# File 'lib/sorted/set.rb', line 125 def <=>(other_set) @ary <=> other_set.to_a end |
#assoc(item) ⇒ Object
Searches through an array whose elements are also arrays comparing item with the first element of each contained array using item.==.
Returns the first contained array that matches (that is, the first associated array), or nil if no match is found.
146 147 148 |
# File 'lib/sorted/set.rb', line 146 def assoc(item) @ary.assoc(item) end |
#at(index) ⇒ Object
Returns key, order array pair at index index
153 154 155 |
# File 'lib/sorted/set.rb', line 153 def at(index) @ary.at(index) end |
#direction_intersect(other_set) ⇒ Object
Returns a new array containing elements common to the two arrays, excluding any duplicates.
Any matching keys at matching indexes with the same order will have the order reversed.
a = Sorted::Set.new([['email', 'asc'], ['name', 'asc']])
b = Sorted::Set.new([['email', 'asc'], ['phone', 'asc']])
s = a.direction_intersect(b)
s.to_a #=> [['email', 'desc'], ['phone', 'asc'], ['name', 'asc']]
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/sorted/set.rb', line 43 def direction_intersect(other_set) self.class.new.tap do |memo| unless other_set.keys.empty? a(memo, other_set) b(memo, other_set) end c(memo) d(memo, other_set) end end |
#each ⇒ Object
Calls the given block once for each element in self, passing that element as a parameter.
An Enumerator is returned if no block is given.
16 17 18 19 |
# File 'lib/sorted/set.rb', line 16 def each return to_enum(:each) unless block_given? @ary.each { |item| yield item } end |
#keys ⇒ Object
27 28 29 |
# File 'lib/sorted/set.rb', line 27 def keys @ary.transpose.first || [] end |
#length ⇒ Object Also known as: size
Returns the number of elements in self. May be zero.
180 181 182 |
# File 'lib/sorted/set.rb', line 180 def length @ary.length end |
#reject ⇒ Object
Returns a new set containing the items in self for which the given block is not true.
If no block is given, an Enumerator is returned instead.
116 117 118 119 |
# File 'lib/sorted/set.rb', line 116 def reject return to_enum(:reject) unless block_given? self.class.new(@ary.reject { |item| yield item }) end |
#select ⇒ Object
Returns a new set containing all elements of self for which the given block returns a true value.
If no block is given, an Enumerator is returned instead.
105 106 107 108 |
# File 'lib/sorted/set.rb', line 105 def select return to_enum(:select) unless block_given? self.class.new(@ary.select { |item| yield item }) end |
#to_a ⇒ Object
163 164 165 |
# File 'lib/sorted/set.rb', line 163 def to_a @ary end |
#to_h ⇒ Object
173 174 175 |
# File 'lib/sorted/set.rb', line 173 def to_h @ary.inject({}) { |a, e| a.merge(Hash[e[0], e[1]]) } end |
#uniq ⇒ Object
Returns a new set by removing duplicate values in self.
If a block is given, it will use the return value of the block for comparison.
134 135 136 137 |
# File 'lib/sorted/set.rb', line 134 def uniq return self.class.new(@ary.uniq) unless block_given? self.class.new(@ary.uniq { |item| yield item }) end |