[Ruby] Display today's day of the week using Date class

I am reviewing the drill to strengthen my logical thinking. As a beginner, I would appreciate it if you could let me know if you have any questions.

problem

Write the code to display today's day of the week using the Date class. However, only if it is Friday, please change the displayed contents as follows.

(Output contents) "Today is Monday" "Today is Friday !!!"

answer

ruby.rb


require "date"     #①

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

if day == 5     #④
  puts "today#{days[day]}That's it! !! !!"
else
  puts "today#{days[day]}"
end

(1) Before using the Date class, you need to call the Date class from the Ruby library. The require method is used for this purpose.

(2) The typical acquisition method of the Date class instance (object) is the today method. It will get today's date. And since we want to get the day of the week this time, add the wday method. The return value of the wday method is a number from 0 to 6, and the day of the week is set to an integer from 0 (Sunday) to 6 (Saturday) to get the value. In other words, at the same time as creating an instance with Date.today and getting today's date, wday also gets the day of the week numerically and assigns it to the variable day.

(3) Since the return value of wday is a numerical value, it is necessary to specify the format for displaying the day of the week. Define in order by setting 0 to Sunday according to wday. You're ready to go!

④ Add a conditional bifurcation on Friday. If the conditional expression is false, it will move to the next conditional expression, so let's specify the condition for Friday first.

that's all!

Recommended Posts

[Ruby] Display today's day of the week using Date class
Output of the day of the week using Ruby's Date class
[Ruby] Code to display the day of the week
How to get today's day of the week
Display Japanese calendar and days of the week using java8 standard class
[Java] Calculate the day of the week from the date (Calendar class is not used)
[Ruby] I want to make a program that displays today's day of the week!
When using the constructor of Java's Date class, the date advances by 1900.
[Ruby] Display the contents of variables
[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
[Ruby] Summary of class definitions. Master the basics.
Zeller's official (asking for the day of the week)
I want to output the day of the week
[Ruby] Class nesting, inheritance, and the basics of self
Try using the query attribute of Ruby on Rails
[Java] How to display the day of the week acquired by LocalDate, DateTimeformatter in Japanese
Getting a little stuck on the last day of the month using Java's Calendar class
Example of using abstract class
(Ruby on Rails6) Display of the database that got the id of the database
[Ruby] Creating code using the concept of classes and instances
[Java] Get date information 10 days later using milliseconds in the Date class
Change the conversation depending on which day of the week is today
About the behavior of ruby Hash # ==
Get date without using Calendar class
[Ruby] Date, Time, Datetime class basics
Various methods of the String class
I want to display the number of orders for today using datetime.
[Ruby] I want to display posted items in order of newest date
[Java] How to get to the front of a specific string using the String class
Iterative processing of Ruby using each method (find the sum from 1 to 10)
[Ruby On Rails] How to search the contents of params using include?
Java unit test Judgment of specified day of the week Determines whether ArDate, Date, Calendar, and long are specified days of the week.
Display text on top of the image
Try using || instead of the ternary operator
[Ruby] Obtaining even values ​​using the even? Method
Test the integrity of the aggregation using ArchUnit ②
Implement the algorithm in Ruby: Day 1 -Euclidean algorithm-
part of the syntax of ruby ​​on rails
[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
[Java] Get the date with the LocalDateTime class
Test the integrity of the aggregation using ArchUnit ③
[Java] Get and display the date 10 days later using the Time API added from Java 8.
Call a method of the parent class by explicitly specifying the name in Ruby
[Ruby] How to calculate the total amount using the initialize method and class variables
Retrieve the first day of week in current locale (what day of the week is it today?)