Sha256: 11001d55c621d70e4fcb41aca1f4eebc39e011b5ad6a90b349d733084d6c7b59

Contents?: true

Size: 1008 Bytes

Versions: 12

Compression:

Stored size: 1008 Bytes

Contents

class RangeArray < Array
  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

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

module RangeArray::Etest
  def ra(*array)
    RangeArray.new array
  end

  def test_range_array
    assert_equal [1..3],                        ra(1, 2, 3)
    assert_equal [1..3, 5..6],                  ra(1, 2, 3, 5, 6)
    assert_equal [1..3, 5..6, 8],               ra(1, 2, 3, 5, 6, 8)
    assert_equal [1..2, 7, 3, 5..6, 8],         ra(1, 2, 7, 3, 5, 6, 8)
    assert_equal [1..2, 7, 3, 5..6, 8, 4],      ra(1, 2, 7, 3, 5, 6, 8, 4)
    assert_equal [1..2, 7, 3, 5..6, 8, 4, -3],  ra(1, 2, 7, 3, 5, 6, 8, 4, -3)
  end
end if VEX_TEST == "base"

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
vex-0.6.2 lib/vex/base/range_array.rb
vex-0.4.4 lib/vex/base/range_array.rb
vex-0.4.2 lib/vex/base/range_array.rb
vex-0.3.3 lib/vex/base/range_array.rb
vex-0.2.9 lib/vex/base/range_array.rb
vex-0.2.8 lib/vex/base/range_array.rb
vex-0.2.7 lib/vex/base/range_array.rb
vex-0.2.6 lib/vex/base/range_array.rb
vex-0.2.5 lib/vex/base/range_array.rb
vex-0.2.2 lib/vex/base/range_array.rb
vex-0.2.1 lib/vex/base/range_array.rb
vex-0.2 lib/vex/base/range_array.rb