class Range
Public Class Methods
optimize(ranges)
click to toggle source
Takes an array of ranges, and returns a new array where all overlapping ranges are combined into a single range
# File lib/epitools/core_ext/range.rb, line 70 def self.optimize(ranges) ranges = ranges.sort_by(&:first) result = [ranges.first] ranges[1..-1].each do |elem| if result[-1].overlaps?(elem) result[-1] = (result[-1] | elem) else result << elem end end result end
Public Instance Methods
&(other)
click to toggle source
Return a new range which is the intersection of the two ranges
# File lib/epitools/core_ext/range.rb, line 28 def &(other) mins, maxes = minmax.zip(other.minmax) (mins.max..maxes.min) end
actual_last()
click to toggle source
The actual last value in the range (eg: ‘(1…10).actual_last == 9`)
# File lib/epitools/core_ext/range.rb, line 6 def actual_last exclude_end? ? last - 1 : last end
merge(other)
click to toggle source
Merge this range with another (if the two ranges overlap, then it returns an array containing a single merged range; if the two ranges are disjoint, an array with the two ranges is returned)
# File lib/epitools/core_ext/range.rb, line 51 def merge(other) if self.overlaps?(other) [self | other] else [self, other] end end
mid()
click to toggle source
The number in the middle of this range.
# File lib/epitools/core_ext/range.rb, line 20 def mid (min + max) / 2 end
Also aliased as: middle
overlaps?(other)
click to toggle source
Test if this range overlaps the other
# File lib/epitools/core_ext/range.rb, line 62 def overlaps?(other) # overlap == start < finish' AND start' < finish self.first <= other.actual_last and other.first <= self.actual_last end
rand()
click to toggle source
Pick a random number from the range.
# File lib/epitools/core_ext/range.rb, line 13 def rand Kernel.rand(self) end
|(other)
click to toggle source
Return a new range which is the union of the two ranges (even if the two ranges don’t overlap)
# File lib/epitools/core_ext/range.rb, line 37 def |(other) vals = [ first, other.first, actual_last, other.actual_last ].sort (vals.first..vals.last) end