Sha256: d6317d9c266ba1d9e5061894b76542b3a20611266a1f721d33018e142c819e24

Contents?: true

Size: 942 Bytes

Versions: 1

Compression:

Stored size: 942 Bytes

Contents

=begin rdoc

= Infinity

A full featured Infinity class, supporting aleph levels and signed 
direction. Inifinty is a multiton based on the aleph and direction 
values. The conastant INFINITY is provided as the common case with 
aleph=0 and direction=+1 (positive).

= Author(s)

* Thomas Sawyer

=end


require 'carat/multiton'


class Infinity < Numeric
  include Multiton

  def self.[](a)
    self.new(a)
  end
  
  attr_reader :aleph, :direction
    
  def initialize(aleph=0, direction=1)
    @aleph = aleph
    @direction = direction
  end
  
  def -@
    self.class.instance(@aleph, @direction * -1)
  end
  
  def to_f
    (1.0/0) * @direction
  end
  
  def times
    loop do
      yield
    end
  end

  def <=>(x)
    return (x.kind_of?(Infinity) ? (@aleph <=> x.aleph) : @direction)
  end
  
  def to_s
    %Q{#{ '-' if @direction == -1 }Infinity[#{@aleph}]}
  end
  
end

# constant (for aleph=0)
INFINITY = Infinity.instance


Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
carats-0.3.0 lib/carat/infinity.rb