October 2006
Intermediate to advanced
888 pages
16h 55m
English
Sometimes for a given month and year, we want to find the date of the third Monday in the month, or the second Tuesday, and so on. The code in Listing 7.1 makes that calculation simple.
If we are looking for the nth occurrence of a certain weekday, we pass n as the first parameter. The second parameter is the number of that weekday (0 meaning Sunday, 1 meaning Monday, and so on). The third and fourth parameters are the month and year, respectively.
def nth_wday(n, wday, month, year) if (!n.between? 1,5) or (!wday.between? 0,6) or (!month.between? 1,12) raise ArgumentError end t = Time.local year, month, 1 first = t.wday if first == wday fwd = 1 elsif first < wday fwd ... |
Read now
Unlock full access