Skip to content

Conversation

@seicho
Copy link
Owner

@seicho seicho commented Aug 29, 2023

カレンダープログラムのレビュー依頼となります。確認お願いします。

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.

コメントしました。


(1..date.day).each do |x|
if x == today_day
print "\e[31m\e[47m#{x.to_s.rjust(3)}\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

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。修正しました。

end
end

#コマンドラインオプションに応じてprint_calenderに渡す引数を定義

Choose a reason for hiding this comment

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

慣れてきてからでいいですが、コメントは基本的に必要な箇所でのみ書くようにするといいかなと思います!

書かない

  • コードと同じことを日本語で言い換えているだけのコメント
    1 + 2 # 1と2を足す
  • コード中に書いてもしょうがないこと
    # いつかリファクタリングする
    これきっかけでやることはほぼないので、タスク管理ツールにでも書きましょう。
    せめて TODO: とつけておくと、ツールなどでTODOだけ抽出したりすることはできます。

書くとよい

基本的にコードから読み取れないことを書きます。

  • 目的や背景などの説明

    # ○○のために必要
    def xxx

    これもメソッド名などをそのままの言い換えているだけなら不要

  • インターフェースの説明

    # 引数aは { name: String, age: Integer } という形を想定している
    def xxx(a)
    • こちらはrdocyardなどの形式で書くと汎用性が高いです。
    • rbsの型情報として書くという手もあります
  • 設計判断のコンテキスト

    # 通常○○を使うが、ここでは△△が重要になってくるため、こういう設計にした

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。コードを見てわかるコメントは削除しました。

end

#1-12以外の月が入力された際の処理
def isMonthValueInvalid?(parameters)

Choose a reason for hiding this comment

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

  • Rubyのメソッド名は snake_case で命名するようにしましょう
  • ? でおわるメソッドは truefalse を返すことが期待されます。今回は条件でプログラムの実行を中止するものなので、 validate_month くらいでいいかと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。修正しました。

Comment on lines 54 to 65
if parameters["y"] && parameters["m"]
isMonthValueInvalid?(parameters)
date = Date.new(parameters["y"].to_i,parameters["m"].to_i,-1)
indent_for_first_week = Date.new(parameters["y"].to_i,parameters["m"].to_i,1).wday
elsif parameters["m"]
isMonthValueInvalid?(parameters)
date = Date.new(Date.today.year,parameters["m"].to_i,-1)
indent_for_first_week = Date.new(Date.today.year,parameters["m"].to_i,1).wday
else
date = Date.new(Date.today.year,Date.today.month,-1)
indent_for_first_week = Date.new(Date.today.year,Date.today.month,1).wday
end

Choose a reason for hiding this comment

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

条件分岐はできるだけ局所的になるようにすると、コードの冗長な部分も共通化でき、見通しもよくなります。

input_year = parameters["y"]
input_month = parameters["m"]
validate_month(input_month) if input_month
today = Date.today

year = input_year || today.year
month = input_month || today.month

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

上のように変数にとる値を条件で変えて、共通部分は変数を使う、とするとよいかと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。
このように記載するとすっきりするんですね

["日","月","火","水","木","金","土"].each do |x|
print x.rjust(2)
end
print "\n"

Choose a reason for hiding this comment

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

改行の表示だけであれば引数なしの puts が手軽かなと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。修正しました。

today_day = Date.today.day
end

(1..date.day).each do |x|

Choose a reason for hiding this comment

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

Date のオブジェクトも Range にできますので、 (first_day..last_day).each do |date| みたいにもできます。

Copy link
Owner Author

Choose a reason for hiding this comment

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

アドバイス頂いた通り、修正しました。

Comment on lines 35 to 36
counter += 1
if counter == 7

Choose a reason for hiding this comment

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

土曜日比較だけですかね。それであれば剰余計算などでも代替できると思います。
(first_line_indent + x) % 7 == 0 とかですかね。(試してないので若干違うかもしれません)
そもそも上記の通り、 Date オブジェクトでのループにしておけば date.saturday? などで十分になります。

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.

コメントしました。


first_line_indent.times do |x|
print "".rjust(3)
counter += 1

Choose a reason for hiding this comment

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

counter不要になってそうですね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

コメントありがとうございます。修正しました。

counter += 1
end

if first_day.year == Date.today.year && first_day.month == Date.today.month

Choose a reason for hiding this comment

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

この条件不要ではないかなと思いました。
たしかに、そもそも月が違うと今日の日付とマッチすることがないので少しメモリなどの計算資源が無駄にはなりますが、微々たるものだと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

DateオブジェクトでRangeにしたので、if x == Date.todayだけでいいですね。
ありがとうございます。

puts

first_line_indent.times do |x|
print "".rjust(3)

Choose a reason for hiding this comment

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

単に " " とか " " * 3 とかでいいかなと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。修正しました。

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です。細かいところ一点だけコメントしました。

end

def validate_month(month)
return unless month.to_i < 1 || month.to_i > 12

Choose a reason for hiding this comment

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

個人の好みもあると思いますが、 unless は条件式がよほど簡単じゃないと逆に直感的ではない感じになるので、

return if 1 <= month.to_i && month.to_i <= 12

という感じでどうかなと思いました。

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.

3 participants