-
Notifications
You must be signed in to change notification settings - Fork 0
カレンダープログラムの作成 #2
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
base: main
Are you sure you want to change the base?
Conversation
02.calendar/cal.rb
Outdated
| else | ||
| print "\n" | ||
| end | ||
| end No newline at end of file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
細かいですが、末尾改行しましょうー
02.calendar/cal.rb
Outdated
| require 'optparse' | ||
|
|
||
| # 今日の日付を取得する | ||
| today_date = Date.today |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
細かいですが、=の後ろは半角空白1つにしましょう
02.calendar/cal.rb
Outdated
|
|
||
| # 今日の日付を取得する | ||
| today_date = Date.today | ||
| date_params = {year: today_date.year, month: today_date.month, day: today_date.day} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
細かいですが、{}の前後空白あけたいですねー
02.calendar/cal.rb
Outdated
| require 'date' | ||
| require 'optparse' | ||
|
|
||
| # 今日の日付を取得する |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
説明的なコメントは原則なくしたいですね。
参考
https://qiita.com/jnchito/items/f0d90af4ed44b7484103
| opt.on('-y [VAL]') {|v| v.to_i} | ||
| opt.on('-m [VAL]') {|v| v.to_i} | ||
|
|
||
| opt.parse!(ARGV, into: cmd_params) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
into知らなかったです。便利ですねー 👍
| else | ||
| date_params[:month] = cmd_params[:m] | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
後述してますが、今日の判定を見直すとsame_ymなくせそうですね。するとこの辺のロジックもすっきりしそうだな、と思います。
02.calendar/cal.rb
Outdated
| #色の反転 | ||
| def reverse_cmpcolor(text) | ||
| "\e[7m#{text}\e[0m" | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
他のところメソッド化してないのにここだけ急にメソッドなことに違和感を感じます。なぜここだけメソッドなのですか?
02.calendar/cal.rb
Outdated
|
|
||
| # カレンダー部分の出力 | ||
| # 余白部分の空白を予め出力しておく | ||
| dow_num = first_date.wday |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dow_numがよくわからない命名だなと思いました。dowってなんですか? date of week? 一般的な略称なんでしょうか? あまり一般的でないなら変に省略すると可読性が下がるかな、と思います。
02.calendar/cal.rb
Outdated
| # カレンダー部分の出力 | ||
| # 余白部分の空白を予め出力しておく | ||
| dow_num = first_date.wday | ||
| dow_num.times { print " " } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
必須対応ではないですが、文字列で掛け算をするとすっきりかけそうです。
02.calendar/cal.rb
Outdated
| dow_num.times { print " " } | ||
|
|
||
| # 日にちの出力 | ||
| (first_date.day..last_date.day).each do |num| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ブロック変数にもっと具体的な名前をつけましょう。numではなんのnumなのかわからないですよね。
02.calendar/cal.rb
Outdated
| if same_ym == true && num == date_params[:day] | ||
| print reverse_cmpcolor(num.to_s.rjust(2)) | ||
| (first_date..last_date).each do |current_date| | ||
| if current_date.year == date_params[:year] && current_date.month == date_params[:month] && current_date.day == date_params[:day] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
today_dateせっかくあるのでうまく活用しましょうー。Date型同士で比較するとシンプルですね
02.calendar/cal.rb
Outdated
| print current_date.day.to_s.rjust(2) | ||
| end | ||
| if dow_num == 6 | ||
| if day_of_week_index == 6 # 曜日リセット |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://docs.ruby-lang.org/ja/latest/method/Date/i/saturday=3f.html というのが全曜日分あったりします。このへんうまく使うとday_of_week_indexは無くせるのではないかな、と思いました。いかがでしょうか。
This reverts commit f340860.
02.calendar/cal.rb
Outdated
|
|
||
| # 日にちの出力 | ||
| (first_date..last_date).each do |current_date| | ||
| if current_date.year == today_date.year && current_date.month == today_date.month && current_date.day == today_date.day |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Date型同士っていうのはこういうことじゃないです。これだと年(Integer)、月(Integer), 日(Integer)それぞれで比較ですよね。
No description provided.