Created
August 14, 2012 20:12
-
-
Save slbug/3352429 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment