Skip to content

Conversation

@jepe-w
Copy link
Owner

@jepe-w jepe-w commented Feb 29, 2024

No description provided.

Copy link

@JunichiIto JunichiIto left a comment

Choose a reason for hiding this comment

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

全体的に修正が必要な箇所が多数あるので、提出物のページにコメントします。

また、bug_cafeへのシンボリックリンク?がdiffに含まれています。
これも不要なので削除してください。

end


p point No newline at end of file

Choose a reason for hiding this comment

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

ファイルの末尾に赤丸が出ないようにしてください。
https://docs.komagata.org/5676#crlf

あと、pメソッドはデバッグ目的で使うことが多いメソッドです。
通常のターミナル出力はputsまたはprintを使ってください。

Copy link

@JunichiIto JunichiIto left a comment

Choose a reason for hiding this comment

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

コメントしました!

また、前回コメントした「また、bug_cafeへのシンボリックリンク?がdiffに含まれています。
これも不要なので削除してください。」が対応できてないようなので再確認をお願いします。
Screenshot 2024-03-11 at 9 36 49

#!/usr/bin/env ruby

score = ARGV[0]
score = score.split(',')

Choose a reason for hiding this comment

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

再代入を避け、別の変数名を付けましょう
https://bootcamp.fjord.jp/pages/avoid-re-assign

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 30 to 31
frames[9] += frames[10]
frames.pop

Choose a reason for hiding this comment

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

Suggested change
frames[9] += frames[10]
frames.pop
frames[9] += frames.pop

でよいですね

end

point = 0
count = 0

Choose a reason for hiding this comment

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

each_with_indexメソッドを使えばcount変数をなくせるはずですー

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます!
こういうのをパット考えれるようになりたい。。

count = 0
frames.each do |frame|
point += if frame.length == 2 && frame[0] == 10
if frames[count + 1].length == 3

Choose a reason for hiding this comment

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

next_frame = frames[count + 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.

ありがとうございます!
変数の命名が今の自分の課題の一つだと思っていますが、
今回の件をきっかけに良い記事を見つけて、少し変数の命名に幅が広がった気がします。
日々勉強を重ねて今後はこう言った小さいけれどとても大切なことや気遣いを意識しながら書いていきたいです。

frames.each do |frame|
point += if frame.length == 2 && frame[0] == 10
if frames[count + 1].length == 3
10 + frames[count + 1][0] + frames[count + 1][1]

Choose a reason for hiding this comment

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

Suggested change
10 + frames[count + 1][0] + frames[count + 1][1]
10 + frames[count + 1][0..1].sum

みたいに書くこともできます

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。
採用させていただきました!笑

elsif frames[count + 1][0] == 10
10 + 10 + frames[count + 2][0]
else
10 + frames[count + 1][0] + frames[count + 1][1]

Choose a reason for hiding this comment

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

Suggested change
10 + frames[count + 1][0] + frames[count + 1][1]
10 + frames[count + 1].sum

elseに入ってるならlengthが2であることは決定しているので、こんなふうに書けますね

Copy link

@JunichiIto JunichiIto left a comment

Choose a reason for hiding this comment

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

コメントしました!

# frozen_string_literal: true

score = ARGV[0]
split_score = score.split(',')

Choose a reason for hiding this comment

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

配列なので複数形にした方がわかりやすいです。
あと、splitは動詞なので、split_scoreだと「スコアを分割する」というメソッドに見えます。
https://qiita.com/jnchito/items/459d58ba652bf4763820

splitした、という意味なら splited_scores みたいな名前になりますが、そもそもここはシンプルに

Suggested change
split_score = score.split(',')
scores = ARGV[0].split(',')

で良さそうです

Copy link
Owner Author

Choose a reason for hiding this comment

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

なるほどです。
こういう単純そうに感じることでも言われてようやく気が付きます。。🥲
思考の柔軟性がたりないのでしょうか、、

Choose a reason for hiding this comment

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

思考の柔軟性がたりないのでしょうか、、

最初から完璧に書ける人はいないので大丈夫です!
僕なんか20年プログラマやってますからね。単純に経験値だけ見てもきっと雲泥の差があると思います。
(同じコードを見ても、jepeさんには見えない何かが僕にはたくさん見えてるはず)

過去に同じようなテーマでブログを書いたので参考にしてみてください。
https://blog.jnito.com/entry/2020/08/24/083030

リーダブルコードやCODE COMPLETEみたいに「読みやすくてわかりやすいコードの書き方」に特化した技術書もあるので、そうした本を読んでみるのもお勧めです!

Comment on lines 17 to 21
shots << if s == 'X'
10
else
s.to_i
end

Choose a reason for hiding this comment

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

Suggested change
shots << if s == 'X'
10
else
s.to_i
end
shots << (s == 'X' ? 10 : s.to_i)

三項演算子の方がすっきりしそうです

さらにいうと、ロジック的にはここまで短くできるかも?

split_score.each do |s|
  shots << (s == 'X' ? 10 : s.to_i)
  shots << 0 if shots.length < 18 && s == 'X'
end

Copy link
Owner Author

Choose a reason for hiding this comment

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

3項演算子は理解できていたつもりになっていましたが、このように書けるのは正直思いもしませんでした。
こういったテクニックを教えていただけるのはとても良い経験となります😁

Comment on lines 25 to 28
frames = []
shots.each_slice(2) do |s|
frames << s
end

Choose a reason for hiding this comment

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

Suggested change
frames = []
shots.each_slice(2) do |s|
frames << s
end
frames = shots.each_slice(2).to_a

でOKです

Comment on lines 32 to 33
point = 0
frames.each_with_index do |frame, i|

Choose a reason for hiding this comment

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

Suggested change
point = 0
frames.each_with_index do |frame, i|
point = frames.each_with_index.sum do |frame, i|

sumメソッドを使ってpoint +=をなくしてみましょう

point = 0
frames.each_with_index do |frame, i|
next_frame = frames[i + 1]
not_last_frame = frame.length == 2

Choose a reason for hiding this comment

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

Suggested change
not_last_frame = frame.length == 2
not_last_frame = i != 9

とした方がコードの意図が明確になりそうです

Copy link

@JunichiIto JunichiIto left a comment

Choose a reason for hiding this comment

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

いくつかコメントしましたが大きな問題ではないのでこれでOKです!🙆‍♂️

Comment on lines +12 to +15
frames = shots.each_slice(2).to_a

frames[9] += frames.pop if frames.length == 11
point = frames.each_with_index.sum do |frame, i|

Choose a reason for hiding this comment

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

Suggested change
frames = shots.each_slice(2).to_a
frames[9] += frames.pop if frames.length == 11
point = frames.each_with_index.sum do |frame, i|
frames = shots.each_slice(2).to_a
frames[9] += frames.pop if frames.length == 11
point = frames.each_with_index.sum do |frame, i|

こんなふうに改行した方が処理のグルーピングとしてわかりやすいように思いました

Comment on lines +16 to +33
next_frame = frames[i + 1]
not_last_frame = i != 9
strilke = frame[0] == 10
spare = frame.sum == 10

if not_last_frame && strilke
if next_frame.length == 3
10 + next_frame[0..1].sum
elsif next_frame[0] == 10
10 + 10 + frames[i + 2][0]
else
10 + next_frame.sum
end
elsif not_last_frame && spare
frame.sum + next_frame[0]
else
frame.sum
end

Choose a reason for hiding this comment

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

あくまで参考情報ですが、こんなふうに書くこともできます。

Suggested change
next_frame = frames[i + 1]
not_last_frame = i != 9
strilke = frame[0] == 10
spare = frame.sum == 10
if not_last_frame && strilke
if next_frame.length == 3
10 + next_frame[0..1].sum
elsif next_frame[0] == 10
10 + 10 + frames[i + 2][0]
else
10 + next_frame.sum
end
elsif not_last_frame && spare
frame.sum + next_frame[0]
else
frame.sum
end
next frame.sum if i == 9
next_frame = frames[i + 1]
strilke = frame[0] == 10
spare = frame.sum == 10
if strilke
if i == 8
10 + next_frame[0..1].sum
elsif next_frame[0] == 10
10 + 10 + frames[i + 2][0]
else
10 + next_frame.sum
end
elsif spare
frame.sum + next_frame[0]
else
frame.sum
end

ポイントは以下の通りです。

  • 最終フレームだけ特別扱いして(next frame.sum if i == 9)、21行目、29行目の条件分岐をシンプルにする
  • if next_frame.length == 3 が真になるのは9フレーム目しかないので、if i == 8 としてしまう(これは好みの問題かも)

Copy link
Owner Author

Choose a reason for hiding this comment

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

なるほど、確かに最終フレームを特別扱いするだけで、
そのほかのコードも理解しやすいように書けるようになっているのですね

特別な処理とそうでない処理を切り分けるというのはいろんなところで使えそうなので
使っていこうと思います!

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