2011-05-21 08:25:03 -04:00
|
|
|
# date.rb: Written by Tadayoshi Funaba 1998-2011
|
2002-05-18 08:41:51 -04:00
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
require 'date_core'
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
class Date
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2006-08-25 19:09:30 -04:00
|
|
|
class Infinity < Numeric # :nodoc:
|
|
|
|
|
|
|
|
include Comparable
|
|
|
|
|
|
|
|
def initialize(d=1) @d = d <=> 0 end
|
|
|
|
|
|
|
|
def d() @d end
|
|
|
|
|
|
|
|
protected :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
|
2008-01-17 07:05:43 -05:00
|
|
|
when Infinity; return d <=> other.d
|
|
|
|
when Numeric; return d
|
2006-08-25 19:09:30 -04:00
|
|
|
else
|
|
|
|
begin
|
|
|
|
l, r = other.coerce(self)
|
|
|
|
return l <=> r
|
|
|
|
rescue NoMethodError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def coerce(other)
|
|
|
|
case other
|
|
|
|
when Numeric; return -d, d
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-03-07 03:44:45 -05:00
|
|
|
def to_f
|
|
|
|
return 0 if @d == 0
|
|
|
|
if @d > 0
|
|
|
|
Float::INFINITY
|
|
|
|
else
|
|
|
|
-Float::INFINITY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-08-25 19:09:30 -04:00
|
|
|
end
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|