Is there a way to express 'infinite time'?

后端 未结 3 475
春和景丽
春和景丽 2021-01-04 04:54

I am using the Range class to represent a range of times. Now I want a range which represents any point after a given time.

I tried (DateTime.now .. nil)

相关标签:
3条回答
  • 2021-01-04 05:15

    It's possible using DateTime::Infinity class:

    future = DateTime.now..DateTime::Infinity.new
    future.include?(1_000_000.years.from_now) #=> true
    
    0 讨论(0)
  • 2021-01-04 05:22

    When you need to represent infinite time, you could use an object from a different class, that you create yourself. Just implement the matching operator and any other methods you use, and then it can be used interchangeably with Range objects.

    class TimeRange
        def initialize(min, max)
        ...
        end
    end
    
    0 讨论(0)
  • 2021-01-04 05:24
    range = (Time.now.to_f .. Float::INFINITY)
    range.include?(Time.now.to_f) # => true
    sleep 1
    range.include?(Time.now.to_f) # => true
    range.include?(Float::INFINITY) # => true
    
    0 讨论(0)
提交回复
热议问题