Sha256: 4d94a74630bb7bad9a2b5e83c4d351a978ef4944abb51ebac1b079c7604d3a73

Contents?: true

Size: 883 Bytes

Versions: 1

Compression:

Stored size: 883 Bytes

Contents

class Range

  #
  # Pick a random number from the range.
  #
  def rand
    Kernel.rand(self)
  end

  #
  # The number in the middle of this range.
  #
  def mid
    (min + max) / 2
  end
  alias_method :middle, :mid

  #
  # Return a new range which is the intersection of the two ranges
  #
  def &(other)
    mins, maxes = minmax.zip(other.minmax)

    (mins.max..maxes.min)
  end

  #
  # Return a new range which is the union of the two ranges
  #
  def |(other)
    mins, maxes = minmax.zip(other.minmax)
    (mins.min..maxes.max)
  end

  #
  # Merge two ranges
  #
  def merge(other)
    if self.overlaps?(other)
      [self | other]
    else
      [self, other]
    end
  end

  #
  # Test if this range overlaps the other
  #
  def overlaps?(other)
    # overlap == start < finish' AND start' < finish
    self.first <= other.last and other.first <= self.last
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
epitools-0.5.95 lib/epitools/core_ext/range.rb