[RUBY] Change the conversation depending on which day of the week is today

【Overview】

1. Conclusion </ b>

2. What is the Date class? </ B>

3. How to write </ b>

4. What I learned from here </ b>

  1. Conclusion

❶ "Date class" ❷ today method ❸wday method ❹ Array ❺ if statement

use!


2. What is Date class?

"Date class" is included in the Ruby library This is a standard function! You need a "require" method to use these things.

If you want to put out today's day of the week, you can get the number of today's day of the week with "Date.today.wday". By combining "today method" and "wday method" (return value "0 ~ 6" is assigned in "Sun ~ Sat") to "Date class", "today" and "day of the week" of the day can be output. I will!
3. How to write

Let's see how to actually program! This time, the tension will rise on Friday, which is ahead of Saturday and Sunday, so Let's make it "special wording only on Friday"! Also, make your holidays special.

Today's day of the week output


require"date" ❶                    

 week = ["Day", "Month", "fire", "water", "wood", "Money", "soil"]❹

 now_week_num = Date.today.wday❷❸
 now_week_char = week[now_week_num]
(Since the subscript of the array is a variable in ❷❸, the subscript number changes every day of the week)
 
❺
if now_weekday_num == 5
 puts "today#{now_week_char}Day of the week! ︎ You have no choice but to go drinking!!!"
elsif now_weekday_num == 6 || now_weekday_num == 0
 puts "today#{now_week_char}It's a day of the week, come on a date ♪"
else
 puts "Still today#{now_week_char}It's a day of the week ... When will the holidays come ..."
end



❶ Call the Date class ❷❸ Assign today's day of the week to the variable now_week_num ❹ An array that can output the day of the week as a subscript ❺ if elsif statement makes Friday and holidays special. Friday is equivalent to "5" in numbers.

In this way "Wording" depending on which day of the week "today" is You can change it!


  1. What I learned from here

This program often appears in games I thought that the story would change depending on the conversation options. It's more complicated, of course, but I think I've touched on the basics.

Recommended Posts