Class: RangeArray

Inherits:
Array show all
Defined in:
lib/vex/base/range_array.rb

Defined Under Namespace

Modules: Etest

Instance Method Summary collapse

Methods inherited from Array

#extract_options!, #insp

Methods included from Array::Average

#avg

Constructor Details

#initialize(array) ⇒ RangeArray

Returns a new instance of RangeArray.


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

def initialize(array)
  min = max = nil

  array.each do |i|
    if min 
      if    i >= min && i <= max then next
      elsif i == min-1 then min = i
      elsif i == max+1 then max = i
      else
        push min, max
        min = max = i
      end
    else
      min = max = i
    end
  end
  
  push min, max if min
end

Instance Method Details

#push(min, max) ⇒ Object


22
23
24
# File 'lib/vex/base/range_array.rb', line 22

def push(min, max)
  super min == max ? min : min..max
end