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
41 changes: 41 additions & 0 deletions 02.calendar/cal.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env ruby

require "date"
require "optparse"

options = ARGV.getopts("y:m:")
year = (options["y"] || Date.today.year.to_s).to_i
month = (options["m"] || Date.today.month.to_s).to_i

first_day = Date.new(year, month, 1)
last_day = Date.new(year, month, -1)

# タイトルを出力
puts (month.to_s + "月" + " " + year.to_s).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.

ヘッダー行の組み立てを確認してください。

puts (month.to_s + "月" + " " + year.to_s).center(20)

center(20) の幅 20 ですが、calコマンドの出力では曜日行「日 月 火 水 木 金 土」が20文字(半角換算)あり、ヘッダーはその幅に合わせて中央揃えされます。日本語文字は全角なので、center に渡す幅の計算に注意が必要です。

提出物エビデンスの出力を見ると 11月 2026 となっており、calコマンドの 11月 2026 と大体合っているように見えますが、Rubyの center はバイト数や文字数の扱いが全角/半角混在時に想定と異なる場合があります。さまざまな月(1桁月・2桁月)で 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.

提出エビデンスはターミナルの出力結果のテキストをコピペしただけでしたので、提出エビデンスの貼り付け先のWebの使用フォントにより微妙にヘッダー部分と日のデータ部分がずれているように見えておりました。先ほど、ターミナルの出力結果の画像キャプチャを貼り付けました。ヘッダー部分と日のデータ部分はずれておりません。また、calコマンドと同じ出力結果となっております。


# 曜日を出力
puts "日 月 火 水 木 金 土"

# 日を出力
(first_day..last_day).each do |d|
if d.day == 1

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

1日目のインデント計算で 2 + first_day.wday * 3 としていますが、これは少し崩れる場合があります。

他の日は rjust(3) で幅3を使っています(日曜日だけ rjust(2))。1日目も同じ幅3を基準に考えると、インデントは first_day.wday * 3 ですが、日曜始まりのとき(wday == 0)は rjust(2) にする必要があります。

calコマンドと実際に出力を比較して、特に日曜始まりの月(例:2026年2月など)で幅がズレていないか確認してみてください。

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.

1日目のインデント計算を「2 + first_day.wday * 3」としております。
自分の考えは以下の通りです。

first_dayは、Dateオブジェクトです。
「2 + first_day.wday * 3」の部分の計算は以下となります。
1日目が日曜の場合、rjust(2 + 0 * 3) = rjust(2) <= 日曜だけ幅2
1日目が月曜の場合、rjust(2 + 1 * 3) = rjust(5) <= 幅3
1日目が火曜の場合、rjust(2 + 2 * 3) = rjust(8) <= 幅3
1日目が水曜の場合、rjust(2 + 3 * 3) = rjust(11) <= 幅3
1日目が木曜の場合、rjust(2 + 4 * 3) = rjust(14) <= 幅3
1日目が金曜の場合、rjust(2 + 5 * 3) = rjust(17) <= 幅3
1日目が土曜の場合、rjust(2 + 6 * 3) = rjust(20) <= 幅3

故に日曜だけ幅2となることを考慮しております。
この部分については現状としたいと思います。

print d.day.to_s.rjust(2 + first_day.wday * 3)
if d.saturday?
print "\n"
end
next
end

if d.saturday?
print d.day.to_s.rjust(3) + "\n"
elsif d.sunday?
print d.day.to_s.rjust(2)
else
print d.day.to_s.rjust(3)
end

if d == last_day
print "\n"
end
end