I will summarize what I learned about how to display the day of the week.
Ruby 2.6.5
To display today's day of the week, we use the Ruby standard library called Date class.
require "date"
To get today's day of the week using the Date class, write: wday is a method provided in the Date class, and you can get the day of the week as an integer 0 (Sunday) to 6 (Saturday). Assign to the variable day.
day = Date.today.wday
Prepare an array of days of the week so that it corresponds to the integer obtained by wday.
days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
#Integer 0 1 2 3 4 5 6
I will output it as follows. Extract the day th element from the array called days.
puts "today#{days[day]}is"
Output result (for Monday)
Today is Monday
that's all
Recommended Posts