Skip to content

Instantly share code, notes, and snippets.

@slbug
Created August 14, 2012 20:07
Show Gist options
  • Select an option

  • Save slbug/3352375 to your computer and use it in GitHub Desktop.

Select an option

Save slbug/3352375 to your computer and use it in GitHub Desktop.

Revisions

  1. slbug created this gist Aug 14, 2012.
    95 changes: 95 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,95 @@
    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
    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
    end
    end

    class Date
    include InfiniteComparable
    end

    class DateTime
    include InfiniteComparable
    end

    class Time
    include InfiniteComparable
    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))