-
Notifications
You must be signed in to change notification settings - Fork 0
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
カレンダーのプログラム(ruby) #4
Conversation
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/calendar.rb
Outdated
end | ||
end | ||
|
||
calendar(year, month) |
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.
calendar(year, month) | |
calendar(year.to_i, month.to_i) |
として、calendarメソッドは数値のyearとmonthしか受け取らない、とした方がメソッドの設計として一貫性があっていいかも、と思いました。
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.
おおお、なるほど! メソッド実行時に to_i
すると、受け取り方を限定するという意味合いになるんですね!
👀 からウロコです〜!
そして、そうすると def 内で to_i
してる2行も消せて、良い……! ef74d62
02.calendar/calendar.rb
Outdated
year = options['y'] | ||
year ||= Date.today.year | ||
month = options['m'] | ||
month ||= Date.today.month |
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.
変数の寿命をできるだけ短くするには?というのを考えてみてください〜。
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.
変数の寿命を短くすること、今まで自分になかった観点で、確かにッ!!と思いました! 🙏 😭
定義と実行がなるべく近い場所で行われていると、見通しがよく読みやすいし安全なんですね。
メソッド実行の直前に変数の定義を持って行ってみました! 90e976c
02.calendar/calendar.rb
Outdated
(1..Date.new(year, month, -1).day).each do |day| | ||
date = Date.new(year, month, 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.
(1..Date.new(year, month, -1).day).each do |day| | |
date = Date.new(year, month, day) | |
(Date.new(year, month, 1)..Date.new(year, month, -1)).each do |date| |
として、Dateオブジェクトのままループさせるとロジックもシンプルになっていいかもしれません。
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オブジェクトのままループさせるとロジックもシンプルになっていいかもしれません。
わー、ホントだ!気づかなかった…!!良い! 🙏
なぜかループさせる時は数字にしなきゃいけないような気になっちゃってましたが、そもそも日付は日付のまま扱って回した方が話がシンプルになるんですね。
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.
5333679 にて対応
02.calendar/calendar.rb
Outdated
puts "日 月 火 水 木 金 土" | ||
(1..Date.new(year, month, -1).day).each do |day| | ||
date = Date.new(year, month, day) | ||
date.cwday.times { print "\s" * 3 } if day == 1 |
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.
-m 5
を引数に与えると・・・??
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.
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.
8ea3f9c にて対応
02.calendar/calendar.rb
Outdated
(1..Date.new(year, month, -1).day).each do |day| | ||
date = Date.new(year, month, day) | ||
date.cwday.times { print "\s" * 3 } if day == 1 | ||
print "\s" if day / 10 == 0 |
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.
右寄せはrjustメソッドを使うのも一手です。
https://docs.ruby-lang.org/ja/latest/method/String/i/rjust.html
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.
こっちの方がスマートですね 😄 44dff6b にてやってみました
02.calendar/calendar.rb
Outdated
date = Date.new(year, month, day) | ||
date.cwday.times { print "\s" * 3 } if day == 1 | ||
print "\s" if day / 10 == 0 | ||
print "#{ date == Date.today ? "\e[30;47m#{day}\e[0m" : 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.
print "#{ date == Date.today ? "\e[30;47m#{day}\e[0m" : day }" | |
print date == Date.today ? "\e[30;47m#{day}\e[0m" : 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.
本当だ!逆になぜ修正前のようにしないとダメだと思ったんだろうか… 😹
Co-authored-by: Junichi Ito <jit@sonicgarden.jp>
Co-authored-by: Junichi Ito <jit@sonicgarden.jp>
@JunichiIto レビューありがとうございました!!:pray: めちゃくちゃためになり大変嬉しいです! |
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/calendar.rb
Outdated
year = options['y'] | ||
year ||= Date.today.year |
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.
year = options['y'] | |
year ||= Date.today.year | |
year = options['y'] || Date.today.year |
でもいけそうです。
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.
おおお、本当だ! こう書くと、 ||
の左辺があれば左辺で、なかったら右辺になるから、これで良いのか〜
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.
cd9fae9 にて対応
02.calendar/calendar.rb
Outdated
puts "日 月 火 水 木 金 土" | ||
(Date.new(year, month, 1)..Date.new(year, month, -1)).each do |date| | ||
day = date.day | ||
date.cwday.times { print "\s" * 3 } if day == 1 && date.cwday < 7 |
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の公式リファレンスを見るとcwdayよりいいメソッドが見つかるはず・・・。
それを使うと&& date.cwday < 7
をなくせます!
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の公式リファレンスを見るとcwdayよりいいメソッドが見つかるはず・・・。
それを使うと&& date.cwday < 7をなくせます!
これが難問で、cwday
をまるっと代替できるより良いものを探したんですが、見つけられず…… 😵💫
&& date.cwday < 7
を && !date.sunday?
に変えた方がわかりやすいかも?とは思ったので、そっちは a9660ed でやってみたんですが、多分もっと全てを解決するようなメソッドがあるんだろうなあと思いつつ…… wakaran……
すいません、ひとまず降参ですッ…… 🙏
02.calendar/calendar.rb
Outdated
(Date.new(year, month, 1)..Date.new(year, month, -1)).each do |date| | ||
day = date.day | ||
date.cwday.times { print "\s" * 3 } if day == 1 && date.cwday < 7 | ||
print date == Date.today ? "\e[30;47m#{day.to_s.rjust(2)}\e[0m" : day.to_s.rjust(2) |
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.
day.to_s.rjust(2)
は事前に変数に入れておいた方がDRYになっていいと思います〜。
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.
本当だ! 事前に day を string にしちゃうと、途中で動かなくなる箇所があるに違いないと思いこんでましたが、やってみたら全然動きました 😄
02.calendar/calendar.rb
Outdated
day = date.day | ||
date.cwday.times { print "\s" * 3 } if day == 1 && date.cwday < 7 | ||
print date == Date.today ? "\e[30;47m#{day.to_s.rjust(2)}\e[0m" : day.to_s.rjust(2) | ||
print "\s" |
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.
細かいことを言うと、今の実装だと土曜日も右にスペースがひとつ入っちゃうんですよね〜。
土
7_
みたいな感じで(_
がスペースのイメージ)。
calコマンドの実行結果をテキストエディタにコピペしたりすると、こういうスペースは付いてないはずなので、そこまで再現できるとよりベターです。(見た目には変わらないので必須ではないですが)
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.
確かに!
3aa0fab にて対応してみました!
02.calendar/calendar.rb
Outdated
date.cwday.times { print "\s" * 3 } if day == 1 && date.cwday < 7 | ||
date.cwday.times { print "\s" * 3 } if day == 1 && !date.sunday? |
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の公式リファレンスを見るとcwdayよりいいメソッドが見つかるはず・・・。
それを使うと&& date.cwday < 7をなくせます!
これが難問で、cwday をまるっと代替できるより良いものを探したんですが、見つけられず…… 😵💫
&& date.cwday < 7
を && !date.sunday?
に変えた方がわかりやすいかも?とは思ったので、そっちはやってみたんですが、多分もっと全てを解決するようなメソッドがあるんだろうなあと思いつつ…… wakaran……
すいません、ここだけ降参ですッ…… 🙏
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/class/Date.html を見ると、cwday
によく似た名前でよく似た機能のメソッドがあります。それを使うと・・・?
でヒントになりますかね〜?(リファレンスはもう何度も見た!と言われるかもしれませんが😅)
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.
わーっ、cwday
→ wday
ですね…!!ありがとうございます! 😭
なるほど、頭にcがついているのは、そういうことなのか。
commercial(cwyear = -4712, cweek = 1, cwday = 1, start = Date::ITALY) -> Date
暦週日付に相当する日付オブジェクトを生成します。
週、および週の日 (曜日) は負、または正の数でなければなりません(負のときは最後からの序数)。零であってはなりません。
ありがとうございます!cwday
とwday
のこと、これから忘れないと思います! 😹
@JunichiIto |
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/calendar.rb
Outdated
print "\s" unless date.saturday? | ||
print "\n" if date.saturday? |
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.
この2行三項演算子でまとめられるかも?
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.
おおお!本当だ! 3b2b467 でやってみました! 🙏
Co-authored-by: Junichi Ito <jit@sonicgarden.jp>
@JunichiIto ありがとうございました!だいぶシンプルに見やすくなって、こんなに行数も減るもんなんですね!うれしいです〜 😭 |
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.
確認しました。これでOKです!
よろしくおねがいします 🙏 🙇
カレンダーのプログラム(ruby) | FJORD BOOT CAMP(フィヨルドブートキャンプ)