0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ifと配列の練習

Last updated at Posted at 2023-06-11

今日の曜日(day of week)を表示するコードをDateクラスを使用して書く。

Dateクラス使いたい時は

require"date"

この一文を記述する。

Dateクラス使って今日の曜日を表現したいときは

Date.today.wday

wdayは曜日を0(日曜日)から6(土曜日)の整数で取得することができるDateクラスに用意されているメソッド。

Fridayだった場合だけ表示内容を変えたい。

出力内容は、

「Today is Monday.」

「♪Today is Friday♪」

にしたい。

require "date"
day = Date.today.wday
days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

if day == 5
  puts "♪Today is #{days[day]}♪"
else
  puts "Today is #{days[day]}."
end

となる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?