public
Created

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
require 'date'
require 'time'
require 'active_support/all'
 
p_inf = 1.0 / 0.0
n_inf = -1.0 / 0.0
 
class Date
def <=> (other)
conversion = :"to_#{self.class.name.downcase}"
if other.respond_to?(:infinite?) && other.infinite?
-other.infinite?
elsif other.respond_to?(conversion) && (other.acts_like?(:date) || other.acts_like?(:time))
super other.send(conversion)
else
super other
end
end
end
 
class DateTime
def <=> (other)
conversion = :"to_#{self.class.name.downcase}"
if other.respond_to?(:infinite?) && other.infinite?
-other.infinite?
elsif other.respond_to?(conversion) && (other.acts_like?(:date) || other.acts_like?(:time))
super other.send(conversion)
else
super other
end
end
end
 
class Time
def <=> (other)
conversion = :"to_#{self.class.name.downcase}"
if other.respond_to?(:infinite?) && other.infinite?
-other.infinite?
elsif other.respond_to?(conversion) && (other.acts_like?(:date) || other.acts_like?(:time))
super other.send(conversion)
else
super other
end
end
end
 
class Float
origin_compare = instance_method(:<=>)
 
define_method(:<=>) do |other|
return 0 if other.respond_to?(:infinite?) && infinite? && infinite? == other.infinite?
 
infinite? || origin_compare.bind(self).call(other)
end
end
 
p 1.0 <=> 10.0
 
p Float::INFINITY <=> Float::INFINITY
p -Float::INFINITY <=> -Float::INFINITY
p -Float::INFINITY <=> Float::INFINITY
p Float::INFINITY <=> -Float::INFINITY
p (1.0...10.0).include?((1.0...10.0))
 
time_zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
 
time = Time.now
date = Date.today
datetime = DateTime.now
twz = ActiveSupport::TimeWithZone.new(time, time_zone)
 
 
p(Date.today <=> (Time.now + 10.years))
p(Time.now <=> (Date.today - 10.years))
 
p((Time.now + 10.years) <=> Date.today)
p((Date.today - 10.years) <=> Time.now)
 
p((DateTime.now + 10.years) <=> Date.today)
p((Date.today - 10.years) <=> DateTime.now)
 
p((twz + 10.years) <=> Date.today)
p((Date.today - 10.years) <=> twz)
 
p Range.new(Date.today, p_inf).include?(Time.now + 10.years)
p Range.new(n_inf, Date.today).include?(Time.now - 10.years)
p Range.new(Time.now, p_inf).include?(Date.today + 10.years)
p Range.new(n_inf, Time.now).include?((Date.today - 10.years))
 
p Range.new(Date.today, p_inf).include?(DateTime.now + 10.years)
p Range.new(n_inf, Date.today).include?(DateTime.now - 10.years)
p Range.new(Time.now, p_inf).include?(DateTime.now + 10.years)
p Range.new(n_inf, Time.now).include?((DateTime.now - 10.years))
 
p Range.new(Date.today, p_inf).include?(twz + 10.years)
p Range.new(n_inf, Date.today).include?(twz - 10.years)
p Range.new(Time.now, p_inf).include?(twz + 10.years)
p Range.new(n_inf, Time.now).include?((twz - 10.years))
 
p Range.new(Date.today, p_inf).include?(twz - 10.years)
p Range.new(n_inf, Date.today).include?(twz + 10.years)
p Range.new(Time.now, p_inf).include?(twz - 10.years)
p Range.new(n_inf, Time.now).include?((twz + 10.years))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.