I want to describe today's day of the week using the Date class.
I want to change the display contents only when it is Saturday. Example) "Today is Monday" "Today is Saturday !!"
The Date class is a feature of the Ruby standard library. To use
require "date"
Is described.
When you get today's day of the week
Date.today.wday
It is described as. wday is a method that can get the day of the week as an integer from 0 (Sunday) to 6 (Saturday).
Example)
require "date"
day = Date.today.wday
puts day
#The number according to the day of the week is output
Now that you can output numbers that match the day of the week, how do you output characters?
You can get it by using an array and subscripts.
days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ]
puts days[0]
#Sunday is output
After that, if you consider conditional branching that changes the display only on Saturday, you will get the expected output result.
require "date"
day = Date.today.wday
days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ]
if day == 6
puts "today#{days[day]}That's it! !!"
else
puts "today#{days[day]}"
end
The number of today's day of the week is assigned to day. Sunday to Saturday is assigned to days in the array. By substituting day for days, the specified day of the week can be output from the array, and if the output result is changed using the if statement, the output content can be changed depending on the day of the week output and the day of the week.
Recommended Posts