[Ruby] Change the conversation depending on which day is today

1 minute read


1. Conclusion

2.What is Date class?

3. How to write

4. What I learned from here

  1. Conclusion

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


  1. What is the Date class

    “Date class” is provided in Ruby library Standard function! You will need a “require” method to use this.

If you want to show today’s day of the week, use “Date.today.wday” to get the number of today’s day. By combining “today method” and “wday method (“Sun-Sat” with return value “0-6” assigned)” to “Date class”, you can display “Today” and “Day of the week” of the day. I will!

  1. How to write

    Let’s see how to actually program! This time the tension rises on Friday, which is ahead of Saturday and Sunday, Let’s use “special words only on Friday”! Also make special holidays.

```ruby: Today’s day output require “date” ❶

week = [“Sun”, “Month”, “Tue”, “Wed”, “Thurs”, “Fri”, “Sat”]❹

now_week_num = Date.today.wday❷❸ now_week_char = week[now_week_num] (The subscript of the array is a variable in ❷❸, so the subscript number changes every day of the week) ‥ ❺ if now_weekday_num == 5 puts “Today, I have no choice but to go for a drink on #{now_week_char} day of the week!!! elsif now_weekday_num == 6 || now_weekday_num == 0 puts “Today is #{now_week_char} day, are you on a date?” else puts “Today is still #{now_week_char} day of the week… When will the holidays come…” end

``` ❶ Call Date class ❷❸ Assign today’s day of week to variable now_week_num ❹ A form that can output the day of the week as a subscript in an array ❺ If elsif statement makes Friday and holidays special words. Friday is equivalent to “5”.

In this way, “Word” depending on which “day” is “today” You can change it!

  1. What I learned from here

    This program often comes out in games I thought that the story would change depending on the conversation options. Of course it’s more complicated, but I thought I touched on the basics.