Sha256: d5afbfac9e826664afbf2235a07b3e7c79c150640fef34ee5c48dfab669f5974

Contents?: true

Size: 941 Bytes

Versions: 19

Compression:

Stored size: 941 Bytes

Contents

class Date
  class Infinity < Numeric
    include Comparable

    def initialize(d = 1)
      @d = d <=> 0
    end

    attr_reader :d

    def zero?
      false
    end

    def finite?
      false
    end

    def infinite?
      d.nonzero?
    end

    def nan?
      d.zero?
    end

    def abs
      self.class.new
    end

    def -@
      self.class.new(-d)
    end

    def +@
      self.class.new(+d)
    end

    def <=>(other)
      case other
      when Infinity
        d <=> other.d
      when Numeric
        d
      else
        begin
          l, r = other.coerce(self)
          l <=> r
        rescue NoMethodError
          nil
        end
      end
    end

    def coerce(other)
      case other
      when Numeric
        [-d, d]
      else
        super
      end
    end

    def to_f
      return 0 if @d == 0
      if @d > 0
        Float::INFINITY
      else
        -Float::INFINITY
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
opal-1.8.3.rc1 stdlib/date/infinity.rb
opal-1.8.2 stdlib/date/infinity.rb
opal-1.8.1 stdlib/date/infinity.rb
opal-1.8.0 stdlib/date/infinity.rb
opal-1.8.0.beta1 stdlib/date/infinity.rb
opal-1.7.4 stdlib/date/infinity.rb
opal-1.8.0.alpha1 stdlib/date/infinity.rb
opal-1.7.3 stdlib/date/infinity.rb
opal-1.7.2 stdlib/date/infinity.rb
opal-1.7.1 stdlib/date/infinity.rb
opal-1.7.0 stdlib/date/infinity.rb
opal-1.7.0.rc1 stdlib/date/infinity.rb
opal-1.6.1 stdlib/date/infinity.rb
opal-1.6.0 stdlib/date/infinity.rb
opal-1.6.0.rc1 stdlib/date/infinity.rb
opal-1.6.0.alpha1 stdlib/date/infinity.rb
opal-1.5.1 stdlib/date/infinity.rb
opal-1.5.0 stdlib/date/infinity.rb
opal-1.5.0.rc1 stdlib/date/infinity.rb