Class: Array

Inherits:
Object show all
Defined in:
lib/joffice/kernel/array.rb

Instance Method Summary collapse

Instance Method Details

#to_rangesObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/joffice/kernel/array.rb', line 20

def to_ranges
  array = self.compact.uniq.sort
  ranges = []
  if !array.empty?
    # Initialize the left and right endpoints of the range
    left, right = array.first, nil
    array.each do |obj|
      # If the right endpoint is set and obj is not equal to right's successor
      # then we need to create a range.
      if right && obj != right.succ
        ranges << Range.new(left,right)
        left = obj
      end
      right = obj
    end
    ranges << Range.new(left,right)
  end
  ranges
end