From 437f53b3f94b9833bf1a8c0cf0bdefcae0599acc Mon Sep 17 00:00:00 2001 From: rani-i3 Date: Tue, 29 Aug 2023 14:22:07 +0900 Subject: [PATCH 1/3] added calendar code --- 02.calendar/calendar.rb | 46 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 02.calendar/calendar.rb diff --git a/02.calendar/calendar.rb b/02.calendar/calendar.rb new file mode 100644 index 0000000000..d7cdcc7be6 --- /dev/null +++ b/02.calendar/calendar.rb @@ -0,0 +1,46 @@ +require 'date' +require 'optparse' + +# コマンドラインでparseする +options = {} +OptionParser.new do |opts| + opts.banner = "Usage: ruby calendar.rb [options]" + + opts.on("-m", "--month MONTH", Integer, "Specify the month (1-12)") do |month| + options[:month] = month + end + + opts.on("-y", "--year YEAR", Integer, "Specify the year") do |year| + options[:year] = year + end +end.parse! + +# 今の日付を取る +current_date = Date.today +year = options[:year] || current_date.year +month = options[:month] || current_date.month + +# date enumeratorを作る +date_range = Date.new(year, month, 1)..Date.new(year, month, -1) +dates_enum = date_range.to_enum + +# カレンダーを表示する +puts "\n#{Date::MONTHNAMES[month]} #{year}".center(20) +puts "日 月 火 水 木 金 土" + +# 最初の日に計算する +initial_offset = dates_enum.peek.wday + +# 見やすいのため +print " " * initial_offset + +# 1ヶ月分表示する +dates_enum.each do |date| + print date.day.to_s.rjust(2) + " " + + # 土曜日になったら新しいラインを作る + puts if date.saturday? +end + +puts "\n" + From 7a5e147c148de3d20f1ace7a6004f56e4c64542d Mon Sep 17 00:00:00 2001 From: rani-i3 Date: Mon, 4 Sep 2023 18:54:31 +0900 Subject: [PATCH 2/3] fixed calendar --- 02.calendar/calendar.rb | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/02.calendar/calendar.rb b/02.calendar/calendar.rb index d7cdcc7be6..bf8b441f93 100644 --- a/02.calendar/calendar.rb +++ b/02.calendar/calendar.rb @@ -1,7 +1,6 @@ require 'date' require 'optparse' -# コマンドラインでparseする options = {} OptionParser.new do |opts| opts.banner = "Usage: ruby calendar.rb [options]" @@ -15,32 +14,21 @@ end end.parse! -# 今の日付を取る current_date = Date.today year = options[:year] || current_date.year month = options[:month] || current_date.month -# date enumeratorを作る date_range = Date.new(year, month, 1)..Date.new(year, month, -1) -dates_enum = date_range.to_enum -# カレンダーを表示する -puts "\n#{Date::MONTHNAMES[month]} #{year}".center(20) +puts current_date.strftime("%-m月 %Y").center(20) puts "日 月 火 水 木 金 土" -# 最初の日に計算する -initial_offset = dates_enum.peek.wday +initial_offset = date_range.first.wday -# 見やすいのため print " " * initial_offset -# 1ヶ月分表示する -dates_enum.each do |date| +date_range.each do |date| print date.day.to_s.rjust(2) + " " - # 土曜日になったら新しいラインを作る - puts if date.saturday? + puts if date.wday == 6 end - -puts "\n" - From a267c62b8f852f04e0713f544b5a31099315abdd Mon Sep 17 00:00:00 2001 From: rani-i3 Date: Tue, 5 Sep 2023 11:11:55 +0900 Subject: [PATCH 3/3] fixed calendar 2 --- 02.calendar/calendar.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/02.calendar/calendar.rb b/02.calendar/calendar.rb index bf8b441f93..af53e859e9 100644 --- a/02.calendar/calendar.rb +++ b/02.calendar/calendar.rb @@ -20,7 +20,8 @@ date_range = Date.new(year, month, 1)..Date.new(year, month, -1) -puts current_date.strftime("%-m月 %Y").center(20) +calendar_title = "#{month}月 #{year}" +puts calendar_title.center(20) puts "日 月 火 水 木 金 土" initial_offset = date_range.first.wday