Skip to content

Instantly share code, notes, and snippets.

Created December 18, 2012 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4326248 to your computer and use it in GitHub Desktop.
Save anonymous/4326248 to your computer and use it in GitHub Desktop.
require 'date'
require 'time'
require 'active_support/all'
p_inf = 1.0 / 0.0
n_inf = -1.0 / 0.0
module InfiniteComparable
extend ActiveSupport::Concern
included do
class_exec do
alias_method :old_comp, :<=>
remove_method :<=>
end
end
def <=> (other)
conversion = :"to_#{self.class.name.downcase}"
if other.respond_to?(:infinite?) && other.infinite?
-other.infinite?
elsif other.respond_to?(conversion)
old_comp other.send(conversion)
else
old_comp other
end
end
end
module FloatInfiniteComparable
extend ActiveSupport::Concern
included do
class_exec do
alias_method :old_comp, :<=>
remove_method :<=>
end
end
def <=>(other)
return old_comp(other) if other.class == self.class
return 0 if other.respond_to?(:infinite?) && infinite? && infinite? == other.infinite?
infinite? || old_comp(other)
end
end
class Date
include InfiniteComparable
end
class DateTime
include InfiniteComparable
end
class Time
include InfiniteComparable
end
class BigDecimal
include FloatInfiniteComparable
end
class Float
include FloatInfiniteComparable
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