LoginSignup
0
0

More than 1 year has passed since last update.

曜日を表示するコード

Posted at

復習のメモ。

Dateクラス

Rubyの標準ライブラリ。以下のようにライブラリから呼び出す。

require "date"

Dateクラスを用いて今日の曜日を取得。

Date.today.wday

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

特定の条件だけの出力

今日が金曜日だった場合→今日は金曜日だ!!!
それ以外の曜日の時→今日は月曜日です。

practise.rb
require "date"

day = Date.today.wday

days = ["日", "月", "火", "水", "木", "金", "土"]


if day == 5
  puts "今日は#{days[day]}曜日だ!"
else
  puts "今日は#{days[day]}曜日です"
end

dayが5(金曜日)だった場合かそうでないかを条件分岐する。
今日が金曜日だった場合はday=5となり、daysの5番目の値である金曜日が出力される。
金曜日じゃない場合、例えば今日が木曜日ならday=4でdaysの4番目の値の木曜日が出力される。

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