Sha256: eb369daa0909e2afd97409caa9c1572a15c83817b26d12fdfd518b9f7fc70e09

Contents?: true

Size: 1.14 KB

Versions: 10

Compression:

Stored size: 1.14 KB

Contents

class Range

  # Uses the Range#umbrella method to determine
  # if another Range is _anywhere_ within this Range.
  #
  #   (1..3).within?(0..4)  #=> true
  #
  # CREDIT: Trans

  def within?(rng)
    case rng.umbrella(self)
    when [0,0], [-1,0], [0,-1], [-1,-1]
      return true
    else
      return false
    end
  end

  # Returns a two element array of the relationship
  # between two Ranges.
  #
  # Diagram ...
  #
  #     Relationship     Returns
  #
  #   self |-----------|
  #   r    |-----------|    [0,0]
  #
  #   self |-----------|
  #   r     |---------|     [-1,-1]
  #
  #   self  |---------|
  #   r    |-----------|    [1,1]
  #
  #   self |-----------|
  #   r     |----------|    [-1,0]
  #
  #   self |-----------|
  #   r     |-----------|   [-1,1]
  #
  #   etc.
  #
  # Example:
  #
  #   (0..3).umbrella(1..2)  #=>  [-1,-1]
  #
  # CREDIT: Trans, Chris Kappler

  def umbrella(r)
    s = first <=> r.first
    e = r.last <=> last

    if e == 0
      if r.exclude_end? and exclude_end?
        e = r.max <=> max
      else
        e = (r.exclude_end? ? 0 : 1) <=> (exclude_end? ? 0 : 1)
      end
    end

    return s,e
  end

end

Version data entries

10 entries across 9 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/range/within.rb
facets-3.1.0 lib/core/facets/range/within.rb
facets-3.0.0 lib/core/facets/range/within.rb
facets-2.9.3 lib/core/facets/range/within.rb
facets-2.9.2 src/core/facets/range/within.rb
facets-2.9.2 lib/core/facets/range/within.rb
facets-2.9.1 lib/core/facets/range/within.rb
facets-2.9.0 lib/core/facets/range/within.rb
facets-2.9.0.pre.2 lib/core/facets/range/within.rb
facets-2.9.0.pre.1 lib/core/facets/range/within.rb