Sha256: 7d0c54fc1c0953ad0bb63e099b28083b87ab0db14836d358578123313674f84b

Contents?: true

Size: 1.92 KB

Versions: 20

Compression:

Stored size: 1.92 KB

Contents

require 'corelib/enumerable'

class Range
  include Enumerable

  `def.$$is_range = true;`

  attr_reader :begin, :end

  def initialize(first, last, exclude = false)
    raise ArgumentError unless first <=> last

    @begin   = first
    @end     = last
    @exclude = exclude
  end

  def ==(other)
    %x{
      if (!other.$$is_range) {
        return false;
      }

      return self.exclude === other.exclude &&
             self.begin   ==  other.begin &&
             self.end     ==  other.end;
    }
  end

  def ===(value)
    include? value
  end

  def cover?(value)
    @begin <= value && (@exclude ? value < @end : value <= @end)
  end

  def each(&block)
    return enum_for :each unless block_given?

    current = @begin
    last    = @end

    while current < last
      yield current

      current = current.succ
    end

    yield current if !@exclude && current == last

    self
  end

  def eql?(other)
    return false unless Range === other

    @exclude === other.exclude_end? &&
    @begin.eql?(other.begin) &&
    @end.eql?(other.end)
  end

  def exclude_end?
    @exclude
  end

  alias :first :begin

  alias :include? :cover?

  alias :last :end

  # FIXME: currently hardcoded to assume range holds numerics
  def max
    if block_given?
      super
    else
      `#@exclude ? #@end - 1 : #@end`
    end
  end

  alias :member? :cover?

  def min
    if block_given?
      super
    else
      @begin
    end
  end

  alias member? include?

  def size
    _begin = @begin
    _end   = @end
    _end  -= 1 if @exclude

    return nil unless Numeric === _begin && Numeric === _end
    return 0 if _end < _begin
    infinity = Float::INFINITY
    return infinity if infinity == _begin.abs || _end.abs == infinity

    (`Math.abs(_end - _begin) + 1`).to_i
  end

  def step(n = 1)
    raise NotImplementedError
  end

  def to_s
    `#{@begin.inspect} + (#@exclude ? '...' : '..') + #{@end.inspect}`
  end

  alias inspect to_s
end

Version data entries

20 entries across 20 versions & 3 rubygems

Version Path
opal-0.9.0.rc1 opal/corelib/range.rb
opal-0.9.0.beta2 opal/corelib/range.rb
opal-0.9.0.beta1 opal/corelib/range.rb
opal-0.8.1 opal/corelib/range.rb
opal-0.8.1.rc1 opal/corelib/range.rb
opal-wedge-0.9.0.dev opal/corelib/range.rb
opal-0.8.0 opal/corelib/range.rb
opal-0.8.0.rc3 opal/corelib/range.rb
opal-0.8.0.rc2 opal/corelib/range.rb
opal-0.8.0.rc1 opal/corelib/range.rb
opal-0.8.0.beta1 opal/corelib/range.rb
opal-0.7.2 opal/corelib/range.rb
opal-0.7.1 opal/corelib/range.rb
opal-0.7.0 opal/corelib/range.rb
opal-0.7.0.rc1 opal/corelib/range.rb
opal-0.7.0.beta3 opal/corelib/range.rb
opal-0.7.0.beta2 opal/corelib/range.rb
opal-cj-0.7.0.beta2 opal/corelib/range.rb
opal-cj-0.7.0.beta1 opal/corelib/range.rb
opal-0.7.0.beta1 opal/corelib/range.rb