[Ruby] Code to display the day of the week

Overview

I will summarize what I learned about how to display the day of the week.

environment

Ruby 2.6.5

Contents

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

[Ruby] Code to display the day of the week
[Ruby] Display today's day of the week using Date class
I want to output the day of the week
How to get today's day of the week
[Ubuntu 20.04] Display the day of the week on the date / clock
[Ruby] Display the contents of variables
[Ruby] I want to make a program that displays today's day of the week!
[Java] How to display the day of the week acquired by LocalDate, DateTimeformatter in Japanese
[Java] Get the day of the specific day of the week
How to find the cause of the Ruby error
Zeller's official (asking for the day of the week)
A memorandum to clean up the code Ruby
The story of introducing Ajax communication to ruby
How to display the result of form input
[Ruby] How to find the sum of each digit
I want to display the name of the poster of the comment
Output of the day of the week using Ruby's Date class
How to display the select field of time_select every 30 minutes
(Ruby on Rails6) Display of the database that got the id of the database
[Ruby] Creating code using the concept of classes and instances
I tried to summarize the basic grammar of Ruby briefly
[Ruby] How to retrieve the contents of a double hash
[Rails] How to display the list of posts by category
How to derive the last day of the month in Java
I want to narrow down the display of docker ps
[Ruby] I want to reverse the order of the hash table
About the behavior of ruby Hash # ==
[Ruby] See the essence of ArgumentError
To be aware of easy-to-read code
I tried to solve the problem of "multi-stage selection" with Ruby
[Java1.8 +] Get the date of the next x day of the week with LocalDate
How to display 0 on the left side of the standard input value
Want to know what Ruby n is the power of 2? (Power judgment of 2)
[Rails] How to omit the display of the character string of the link_to method
Change the conversation depending on which day of the week is today
I want to change the value of Attribute in Selenium of Ruby
I tried to assemble from 0 while considering the table design of the day / week / month / overall ranking display function in terms of performance.
Display text on top of the image
The secret to the success of IntelliJ IDEA
I want to display the number of orders for today using datetime.
Implement the algorithm in Ruby: Day 1 -Euclidean algorithm-
part of the syntax of ruby ​​on rails
[Ruby] I tried to diet the if statement code with the ternary operator
[Ruby] I want to extract only the value of the hash and only the key
How to determine the number of parallels
How to switch the display of the header menu for each transition page
[day: 5] I summarized the basics of Java
[Ruby] Cut off the contents of twitter-ads
About the initial display of Spring Framework
Ruby from the perspective of other languages
[Ruby] I want to display posted items in order of newest date
How to sort the List of SelectItem
Iterative processing of Ruby using each method (find the sum from 1 to 10)
Output of the book "Introduction to Java"
How to solve the local environment construction of Ruby on Rails (MAC)!
I tried to make full use of the CPU core in Ruby
The process of introducing Vuetify to Rails
Ability to display a list of products
[Ruby On Rails] How to search the contents of params using include?
Pre-processing to display on the browser (compiler)
[swift5] How to change the color of TabBar or the color of item of TabBar with code