Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions 02.calendar/calendar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'date'
require 'optparse'

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_range = Date.new(year, month, 1)..Date.new(year, month, -1)

calendar_title = "#{month}月 #{year}"
puts calendar_title.center(20)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

スクショを見るとcenterの意味がないように見えますね。

Screenshot 2023-09-05 at 11 30 59

OSのcalコマンドを実行して、それと同じ見た目になるように修正してみてください。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ああーそれは古いスクショだと思います。今のコードの状態だと、こういう形になりました:
Screenshot 2023-09-05 at 11 49 26
centerとかrjustを使いたいと思いましたが、結局同じ結果なのでcenterにしました。

puts "日 月 火 水 木 金 土"

initial_offset = date_range.first.wday

print " " * initial_offset

date_range.each do |date|
print date.day.to_s.rjust(2) + " "

puts if date.wday == 6
end