Hey Philip,
I think that, for weekday names, you have to keep you have to take into account the week (7 days).
For example, let’s say we have a Date object that “A” represents 11/08/2008 and another one “B” that represents 18/08/2008. Both are mondays.
Now, we have a Date object that represents today - 07/08/2008.
If you load Ryan’s code and do this:
A.to_words
The following will happen:
-
Is this date the same as today (Date.today)? If so, output “Today”;
-
Is this date the same as yesterday (Date.today-1)? If so, output “Yesterday”;
-
Is this date included in the set = {today - 7 (one week ago) … yesterday} - this will essentially create an array that has date objects from one week ago until yesterday. If the date is included in this array, and as we have considered it to be “last week” and as we know that the date is not yesterday, nor the current date nor tomorrow, we can say this date was the last “weekday”.
-
Applies the same logic as above, but inverted (one week from now).
Now, things get a little more complicated. For example, the next test says:
elsif ((Date.today + 8)…(Date.today + 14)).include?(self)
“Two #{self.strftime(”%A")}s away"
What does that mean? From what I could understand, the test checks if the Date object being converted is in a period of time that starts one week + 1 from now ( ss ) and ends two weeks - 1 from now. Considering we have the following date object: 14/08/2008 and today is today’s date (07/08/2008):
today + 8 = 15/08/2008
today + 14 = 21/08/2008
S M Tu W Th F S S M Tu W Th { F S S M Tu W Th } F S S …
> > >
Today 14/08/2008 |
21/08/2008
This test wouldn’t catch our date object, but the previous one would do (Next #{weekday}).
But one important point is that he seems to build upon last the previous tests, so I think the order is important, and also explains the odd offsets he does in the checks, the code “considers” something like “hey, if this date is not tomorrow then I can omit tomorrow from the next check…”.
Calculations with Dates are confusing, at least to me. That’s how I could interpret Ryan’s code, and I’m sure it is not a complete view yet. I don’t know if there’s a pattern for such thing or any way to simplify this code, that’s with Ryan!
Please, correct me if I’m wrong!
Marcelo.