[Ruby] [Ruby] Code for displaying the day of the week

less than 1 minute read

Overview

Summarize what you learned about how to display the days of the week.

environment

Ruby 2.6.5

Content

To display today’s day of the week, we use Ruby standard library called Date class.

require "date"

To use the Date class to get today’s day of the week, 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). Substitute for the variable day.

day = Date.today.wday

Prepare an array of days of the week to correspond to the integers obtained by wday.

days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
# Integer 0 1 2 3 4 5 6

Output as below. Extracts the day-th element from the array named days.

puts "Today is #{days[day]}"

Output result (for Monday)

Today is Monday

that’s all

Tags:

Updated: