October 2006
Intermediate to advanced
888 pages
16h 55m
English
There are several ways to determine the day of the week. First, the instance method to_a returns an array of time information. You can access the seventh element, which is a number from 0 to 6 (0 meaning Sunday and 6 meaning Saturday).
time = Time.now day = time.to_a[6] # 2 (meaning Tuesday)
It’s better to use the instance method wday as shown here:
day = time.wday # 2 (meaning Tuesday)
But both these techniques are a little cumbersome. Sometimes we want the value coded as a number, but more often we don’t. To get the actual name of the weekday as a string, we can use the strftime method. This name will be familiar to C programmers. There are nearly two dozen different specifiers that it recognizes, enabling ...
Read now
Unlock full access