Output of the day of the week using Ruby's Date class

What is the Date class?

It is a function of the standard library of Ruby.

To use the Date class, write the following sentence.

require "date"

Then use the Date class

To get "Today's day of the week", write as follows.

day = Date.today.wday

wday is a method provided in the Date class that allows you to get the day of the week as an integer from 0 (Sunday) to 6 (Saturday).

For example, on Thursday, 4 is output!

I thought about how to output "Today is Thursday".


require "date"

day = Date.today.wday

if day == 0
  puts "Today is sunday"
elsif day == 1
  puts "Today is monday"
elsif day == 2
  puts "Today is Tuesday"
elsif day == 3
  puts "Today is wednesday"
elsif day == 4
  puts "Today is thursday"
elsif day == 5
  puts "Today is Friday! !! !!"
else
  puts "Today is saturday"
end

The If statement is long, so it looks like it can be refactored.

The point is that the numbers are output,

It seems that the same result as above can be obtained by substituting the output number into the subscript using the array.


require "date"

day = Date.today.wday
days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]


  puts "today#{days[day]}"


It is compactly organized.

If you use an If statement, it's better to use it when changing comments on a specific day of the week.

I can't write anything to shorten the code yet, so I'd like to remember it while doing it!

Recommended Posts

Output of the day of the week using Ruby's Date class
[Ruby] Display today's day of the week using Date class
[Ubuntu 20.04] Display the day of the week on the date / clock
[Java] Calculate the day of the week from the date (Calendar class is not used)
[Java1.8 +] Get the date of the next x day of the week with LocalDate
[Java] Get the day of the specific day of the week
Display Japanese calendar and days of the week using java8 standard class
How to get today's day of the week
[Ruby] Code to display the day of the week
Use of Date class
Getting a little stuck on the last day of the month using Java's Calendar class
Example of using abstract class
Get date without using Calendar class
Various methods of the String class
Read the first 4 bytes of the Java class file and output CAFEBABE
Change the conversation depending on which day of the week is today
[Java] How to get to the front of a specific string using the String class
Try using || instead of the ternary operator
Test the integrity of the aggregation using ArchUnit ②
[day: 5] I summarized the basics of Java
Output of the book "Introduction to Java"
[Java] Get the date with the LocalDateTime class
Test the integrity of the aggregation using ArchUnit ③
Java unit test Judgment of specified day of the week Determines whether ArDate, Date, Calendar, and long are specified days of the week.
Retrieve the first day of week in current locale (what day of the week is it today?)
[Ruby] Summary of class definitions. Master the basics.
Output of how to use the slice method
About next () and nextLine () of the Scanner class
The date time of java8 has been updated
I tried using the profiler of IntelliJ IDEA
First touch of the Files class (or Java 8)
[Ruby] I want to make a program that displays today's day of the week!
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
[Rails] Sort the post list by date or number of likes using the pull-down box
[Java] How to display the day of the week acquired by LocalDate, DateTimeformatter in Japanese