Forum: Ruby on Rails Every other week recurring event

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
David (Guest)
on 2008-11-11 13:17
(Received via mailing list)
I have the code for computing a weekly event:

(start_date..end_date).select{|d| d.wday == wday}

but I am wondering if anyone has any ideas for using this to figure
out a recurring event for every other week.  Is it possible to somehow
specify every other within the .select method?  Any other
implementation ideas?
Jeff E. (Guest)
on 2008-11-11 20:55
(Received via mailing list)
here's one way - not a one-liner, but not too complex:


start_date = DateTime.parse('2008-01-01')
end_date   = DateTime.parse('2008-04-01')

range = (start_date..end_date)

wday = 1
use_this = false
every_other = []

range.each do |date|
  every_other << date if date.wday == wday && use_this
  use_this = !use_this
end

puts every_other.inspect
This topic is locked and can not be replied to.