Module: RangeOperators::ArrayOperatorDefinitions
- Included in:
- Array
- Defined in:
- lib/range_operators/array_operator_definitions.rb
Overview
Works with integers, dates and strings. However, all the objects in the array must be of the same class.
Instance Method Summary collapse
-
#intersection ⇒ Object
Returns the values in common for an array set (nil, singe value/object, or range).
-
#missing ⇒ Object
Returns the missing elements in an array set.
- #rangify ⇒ Object
Instance Method Details
#intersection ⇒ Object
Returns the values in common for an array set (nil, singe value/object, or range).
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/range_operators/array_operator_definitions.rb', line 42 def intersection array = self.sort_elements array.inject() do |common, element| value_first = comparison_value(common, :last) value_element = comparison_value(element, :first) case value_first <=> value_element when -1 then return nil when 0 then value_first else if element.class == Range value_element..([value_first, comparison_value(element, :last)].min) else value_element end end end end |
#missing ⇒ Object
Returns the missing elements in an array set
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/range_operators/array_operator_definitions.rb', line 61 def missing missing, array = [], self.rangify i, length = 0, array.size - 1 while i < length current = comparison_value(array[i], :last) nextt = comparison_value(array[i+1], :first) missing << (current.succ.succ == nextt ? current.succ : (current.succ)..(nextt - 1)) i += 1 end missing end |
#rangify ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/range_operators/array_operator_definitions.rb', line 20 def rangify array = self.sort_elements array.inject([]) do |collection, value| unless collection.empty? last = collection.last last_value = comparison_value(last, :last) current_value = comparison_value(value, :first) if (last_value.succ <=> current_value) == -1 collection << value else first = comparison_value(last, :first) second = [comparison_value(last, :last), comparison_value(value, :last)].max collection[-1] = [first..second] collection.flatten! end else collection << value end end end |