Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

カレンダープログラムを追加 #2

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

カレンダープログラムを追加 #2

wants to merge 4 commits into from

Conversation

yuu-cc
Copy link
Owner

@yuu-cc yuu-cc commented Nov 2, 2023

No description provided.

Copy link

@yoshitsugu yoshitsugu left a comment

Choose a reason for hiding this comment

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

コメントしました。


opt = OptionParser.new
opt.on('-y VAL') do |v|
v if (1..12).cover?(v.to_i)

Choose a reason for hiding this comment

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

そもそも範囲外の値が渡された場合の想定は今回しなくてよいですが、これだと範囲外の値のときに暗黙的に無視されてしまうので若干ユーザー側からすると不親切かなと思います。
abortexit(1) をしてエラー表示 + エラー終了するほうがベターです。
https://docs.ruby-lang.org/ja/latest/method/Kernel/m/abort.html
https://docs.ruby-lang.org/ja/latest/method/Kernel/m/exit.html

params[:m].to_i
end

class MyCal

Choose a reason for hiding this comment

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

クラス設計についてはオブジェクト指向のプラクティスでやるので、今回はあまりコメントしません。

end

def show
puts "   #{@month}月 #{@year}"

Choose a reason for hiding this comment

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

全角スペースでのフォーマットは避けたほうがよいです。
String#center String#rjust String#ljust あたりが使えないか見てみてください。
https://docs.ruby-lang.org/ja/latest/method/String/i/center.html
https://docs.ruby-lang.org/ja/latest/method/String/i/rjust.html
https://docs.ruby-lang.org/ja/latest/method/String/i/ljust.html

Comment on lines 57 to 59
rescue StandardError
break
end

Choose a reason for hiding this comment

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

これは複数のアンチパターン(よくないとされるパターン)を踏んでいるので、修正おねがいします。

  • エラーを制御構文(if, whileなど)のように使うのはよくないです。エラーは rescue されないとさらに上の呼び出し側に受け渡されますが、これを利用して、C言語などには存在する goto のように一気にループを抜けたりするのに便利に使うこともできますが、そのようなコードが蔓延ってしまうと、どこからどこに行っているのかコード上での把握がかなり困難になってきます。デバッグなどの観点でよくないこととされます。
  • エラーを暗黙的に処理するのはよくないです。特に今使っている StandardError は大部分のエラーも補足してしまいます。そのため、想定外のエラーが発生してもそれを検知できません。

def show
puts "   #{@month}月 #{@year}"
puts '日 月 火 水 木 金 土'
(1..31).each do |n|

Choose a reason for hiding this comment

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

そもそもDateクラスで末日を簡単に作れますので、調べてみてください。
また、Dateクラス自体をRangeとして扱えます。

first_day = Date.new(@year, @month,1)
last_day = Date.new(# どうなるか考えてみてください
(first_day..last_day).each do |date|
  # この中ではdateが各日付をあらわす Dateオブジェクトになります。
end

youbi.times { print ' ' } if n == 1

if d == Date.today
print "\e[30m\e[47m\e[5m#{d.day.to_s.rjust(2)}\e[0m"

Choose a reason for hiding this comment

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

今日の日付での反転に取り組まれていてよいですね 👍
ただ、文字色と背景色を反転させるためのエスケープコードは 7 です。
直接背景色や文字色を指定すると元々の色によっては「反転」にはなりませんので注意してください。
https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_.28Select_Graphic_Rendition.29_parameters

end

if youbi == 6
puts "\n"

Choose a reason for hiding this comment

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

puts は引数なしで改行を出力しますので、

puts

のみで大丈夫です。
https://docs.ruby-lang.org/ja/latest/method/Kernel/m/puts.html

Copy link

@yoshitsugu yoshitsugu left a comment

Choose a reason for hiding this comment

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

コメントしました!

if (1..12).cover?(v.to_i)
v
else
abort "月の指定が正しくありません '#{v}'"

Choose a reason for hiding this comment

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

細かいですが、エラーメッセージのフォーマットは揃えておくほうが見やすいかと思います。
エラー: 正しく処理できない月です にするか、年のほうを月にあわせるほうがよいかと思います。

last_day = Date.new(@year, @month, -1)
(first_day..last_day).each.with_index(1) do |d, n|
youbi = d.wday
youbi.times { print ' ' } if n == 1

Choose a reason for hiding this comment

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

わざわざループの中で条件つけてやる必要はないかと思うので、ループの前でやりましょう。

print d.day.to_s.rjust(2)
end

if youbi == 6

Choose a reason for hiding this comment

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

d.saturday? が使えると思います。
https://docs.ruby-lang.org/ja/latest/method/Date/i/saturday=3f.html

Copy link

@yoshitsugu yoshitsugu left a comment

Choose a reason for hiding this comment

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

必須要件のスクリーンショットにある通り、 ./cal.rb で実行できるようにしてみてください!
参考: https://bootcamp.fjord.jp/articles/40

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

first_day.wday.times { print ' ' }

Choose a reason for hiding this comment

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

これはどちらでもいいのですが、String#* を使って

print '   ' * first_day.wday

と書くこともできますね。
https://docs.ruby-lang.org/ja/latest/method/String/i/=2a.html

Copy link

@yoshitsugu yoshitsugu left a comment

Choose a reason for hiding this comment

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

OKです!

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