Sha256: f71d84506e334e72b7da468e6fc7a736c7f350ca72c04aef66ffefe16dd05c57

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

module RIO
  class Ranges
    def initialize(*args)
      @ranges = []
      args.each { |arg|
        case arg
        when ::Range then @ranges << arg
        else @ranges << Range.new(arg,arg)
        end
      }
    end
    def merge()
      return [] if @ranges.empty
      @ranges.sort_by { |r| r.first }
      st = @ranges[0].start
      en = @ranges[0].end
      @ranges.each { |r|
        break if r.start > en
        en = [en,r.end].max
      }
      
    end
  end
  module RangeMath
    def ceil
      return last - 1 if exclude_end?
      last
    end
    def -(other)
      if include?(other.first) and !include?(other.ceil)
        [Range.new(first,other.first,true)]
      elsif include?(other.ceil) and !include?(other.first)
        [Range.new(other.ceil+1,last,exclude_end?)]
      elsif include?(other.ceil) and include?(other.first)
        [Range.new(first,other.first,true),Range.new(other.ceil+1,last,exclude_end?)]
      else
        [self]
      end
    end
  end
end
class Range
  include RIO::RangeMath
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rio-0.3.3 lib/rio/rangemath.rb