[RUBY] I want to output the day of the week

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

I want to output the day of the week
I want to change the log output settings of UtilLoggingJdbcLogger
[Ruby] I want to make a program that displays today's day of the week!
I want to var_dump the contents of the intent
How to get today's day of the week
[Ruby] Code to display the day of the week
I want to know the answer of the rock-paper-scissors app
I want to display the name of the poster of the comment
Output of the day of the week using Ruby's Date class
I want to be aware of the contents of variables!
I want to return the scroll position of UITableView!
I want to simplify the log output on Android
(ยด-`) .. oO (I want to easily find the standard output "Hello".
I want to expand the clickable part of the link_to method
I want to narrow down the display of docker ps
[Ruby] I want to reverse the order of the hash table
I want to temporarily disable the swipe gesture of UIPageViewController
[Java] Get the day of the specific day of the week
[day: 5] I summarized the basics of Java
Output of the book "Introduction to Java"
I want to understand the flow of Spring processing request parameters
The story of Collectors.groupingBy that I want to keep for posterity
I want to limit the input by narrowing the range of numbers
I want to control the default error message of Spring Boot
I made a tool to output the difference of CSV file
I want to change the value of Attribute in Selenium of Ruby
[Ruby] I want to output only the odd-numbered characters in the character string
I want to display the number of orders for today using datetime.
I want to know the JSP of the open portlet when developing Liferay
[Ruby] I want to extract only the value of the hash and only the key
Output of how to use the slice method
I want to graph the number of photo AC downloads [Scraping implementation] ~ 10 lines per day coding ~
I want to pass the argument of Annotation and the argument of the calling method to aspect
I want to truncate after the decimal point
I want to get the field name of the [Java] field. (Old tale tone)
I want you to use Enum # name () for the Key of SharedPreference
I want to get the value in Ruby
I want to graph the number of photo AC downloads [MySQL ring cooperation] ~ Coding 10 lines a day ~
[RxSwift] I want to deepen my understanding by following the definition of Observable
I want to get the value of Cell transparently regardless of CellType (Apache POI)
I want to control the start / stop of servers and databases with Alexa
I want to separate the handling of call results according to the API caller (call trigger)
I want to see the contents of Request without saying four or five
I want to recursively get the superclass and interface of a certain class
[Ubuntu 20.04] Display the day of the week on the date / clock
[Java] I want to calculate the difference from the date
I want to embed any TraceId in the log
I was addicted to the record of the associated model
I tried to summarize the state transition of docker
Convert the array of errors.full_messages to characters and output
05. I tried to stub the source of Spring Boot
I want to judge the range using the monthly degree
I tried to reduce the capacity of Spring Boot
I want to dark mode with the SWT app
I want to call the main method using reflection
[Rough commentary] I want to marry the pluck method
I want to add a delete function to the comment function
I want to convert characters ...
I want to output the character number from the left where an arbitrary character string appears.
Rails The concept of view componentization of Rails that I want to convey to those who want to quit
[Active Admin] I want to specify the scope of the collection to be displayed in select_box