Reputation: 526
require 'active_support/all'
days = 0.day.ago
days += 1 until days.since.wday == 2
next_tuesday = days.since
Above code is not doing right. But below is right. Could you tell me why?
require 'active_support/all'
current_day = 0.day.ago
current_day += 1.day until current_day.wday == 2
next_tuesday = current_day
Upvotes: 0
Views: 1260
Reputation: 160631
It's possible doing date math using seconds as the interval, but that's old school and I'm sure you don't want to have us old people scare you with stories.
I'm away from my computer so this is untested, but here is the gist of what you want to do:
today = Date.today
today -= today.wday # normalize the day to the first day of the week, AKA Sunday
today += 9 # add a week + 2 days.
It can be stated on one line like:
Date.today - Date.today.wkday + 9
Or something in between the two.
Upvotes: 0
Reputation: 13936
First, mind that you're using ActiveSupport, so this is not pure Ruby. Assuming that, there is an easier way of doing so bundled in ActiveSupport:
Time.now.next_week(:tuesday)
Upvotes: 7