Skip to content

Add my-calendar#2

Open
hiro23900 wants to merge 4 commits into
mainfrom
my-calendar
Open

Add my-calendar#2
hiro23900 wants to merge 4 commits into
mainfrom
my-calendar

Conversation

@hiro23900

Copy link
Copy Markdown
Owner

No description provided.

Comment thread 02.calendar/cal.rb Outdated
default_year = Date.today.year.to_s
default_month = Date.today.month.to_s

options = ARGV.getopts("y:m:", "y:#{default_year}", "m:#{default_month}")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ARGV.getopts の第2引数以降はロングオプションの定義に使うもので、デフォルト値を指定する方法ではありません。

options = ARGV.getopts("y:m:", "y:#{default_year}", "m:#{default_month}")

実際には "y:2026" のような文字列をロングオプション名として解釈しようとするため、意図した動作になっていない可能性があります。

デフォルト値は getopts の戻り値に対して別途設定するのが一般的です。例えば:

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

現在の提出物のエビデンスでは引数なしでも動いているように見えますが、動作原理を確認してみてください。

Comment thread 02.calendar/cal.rb

# 日を出力
(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月など)で幅がズレていないか確認してみてください。

Comment thread 02.calendar/cal.rb
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コマンドと並べて確認してみてください。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants