I know you can use wday to return the day of the week as an integer value:
Date.new(2001,2,3).wday #=> 6 (a.k.a. "Saturday")
But, is there a method to do this the opposite way, in reverse?
Like:
day_of_week(6) #=> "Saturday"
Or is this something I would convert on my own?
Thanks.
The Date::DAYNAMES
array has that information:
Date::DAYNAMES[6]
# => "Saturday"
I would use a proc
or lambda
because I wouldn't want to type this Date::DAYNAMES[d]
repeatedly.
day = Proc.new { |d| Date::DAYNAMES[d] }
day.call(6)
# => "Saturday"
Or even more succinct as a lambda
:
day = ->num { Date::DAYNAMES[num] }
day.(6)
# => "Saturday"
Just remember to: require 'Date'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With