Class: Array

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

Instance Method Summary collapse

Instance Method Details

#to_rangesObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/array.rb', line 2

def to_ranges
  array = self.compact.uniq.sort
  ranges = []
  if !array.empty?
    # Initialize the left and right endpoints of the range
    left, right = self.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