Module: CoreExtensions::Range::Operations::ClassMethods
- Defined in:
- lib/core_extensions/range/operations.rb
Instance Method Summary collapse
-
#merge(*ranges) ⇒ Array<Range>
Merge the given ranges together.
-
#subtract(original_range, *subtracted_ranges, delta: 1) ⇒ Object
Subtract an array of ranges from the given original range.
Instance Method Details
#merge(*ranges) ⇒ Array<Range>
Merge the given ranges together.
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/core_extensions/range/operations.rb', line 74 def merge(*ranges) ranges.sort_by!(&:begin).inject([]) do |merged, range| if merged.empty? || !range.overlaps?(merged.last) merged.append(range) else last_range = merged.pop merged.append(merge_ranges(last_range, range)) end end end |
#subtract(original_range, *subtracted_ranges, delta: 1) ⇒ Object
Subtract an array of ranges from the given original range.
107 108 109 110 111 112 113 114 115 |
# File 'lib/core_extensions/range/operations.rb', line 107 def subtract(original_range, *subtracted_ranges, delta: 1) subtracted_ranges.sort_by!(&:begin).inject([original_range]) do |result, subtracted_range| if result.last&.overlaps?(subtracted_range) result.append(*subtract_overlapping_range(result.pop, subtracted_range, delta: delta)) else result end end end |