Is there a Ruby gem that enables Range calculations similar to what PostgreSQL does? Especially the time-related range functions?
Asked
Active
Viewed 187 times
2 Answers
0
There's Range#cover?
to test if an object is between the begin and end of the range.
Ruby's equivalent to '[2011-01-01,2011-03-01)'::tsrange @> '2011-01-10'::timestamp
is:
(Date.new(2011,01,01)..Date.new(2011,03,01)).cover? Date.new(2011,01,10)
# => true
It tests Date.new(2011,01,01) <= Date.new(2011,01,10) <= Date.new(2011,03,01)
.

Stefan
- 109,145
- 14
- 143
- 218
0
Without knowing exactly what you are trying to accomplish it's hard to answer this question, but you may want to check out the CoreExtensions module of the ActiveSupport gem: http://guides.rubyonrails.org/active_support_core_extensions.html
Install ActiveSupport with gem install activesupport
or add it to your Gemfile.
With ActiveSupport you can do things like this:
require 'active_support/core_ext'
10.days.from_now #=> 2013-06-08 08:37:34 -0400
DateTime.now.beginning_of_year #=> Tue, 01 Jan 2013 00:00:00 -0400
1.megabyte #=> 1048576
(1..10).step(2) #=> [1, 3, 5, 7, 9]
(1..10).overlaps?(0..7) #=> true
Date.current.next_month #=> Sat, 29 Jun 2013
Date.current.prev_week(:friday) #=> Fri, 24 May 2013
I hope this helps.