Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to create a date range in ruby from negative infinity

I'm struggling to define a date range in Ruby (2.6.3) that represents the range of dates up to a given date (in my examples it is Date.today):

BigDecimal("Infinity")...Date.today

*** ArgumentError Exception: bad value for range

nil...Date.today

*** ArgumentError Exception: bad value for range

Date::Infinity.new...Date.today

*** ArgumentError Exception: bad value for range

(Date.today...-Date::Infinity.new)

Fri, 31 May 2019...#

this one doesn't break, but also doesn't appear to give me a meaningful date range:

(Date.today...-Date::Infinity.new).include? Date.yesterday

false

like image 386
Fred Willmore Avatar asked Sep 08 '25 10:09

Fred Willmore


2 Answers

As of ruby 2.7, this is no longer an issue, infinite ranges are handled gracefully:

(..Date.today).cover?(100.years.ago)
=> true
(nil..Date.today).cover?(100.years.ago)
=> true
like image 66
Fred Willmore Avatar answered Sep 10 '25 01:09

Fred Willmore


Maybe not the best solution, but it can be helpful:

(-Float::INFINITY...Date.today.to_time.to_i).include? Date.yesterday.to_time.to_i
 => true
like image 37
Yurii Verbytskyi Avatar answered Sep 10 '25 01:09

Yurii Verbytskyi