Skip to content

cafe.rbのバグを修正 - #48

Open
T-REX1996 wants to merge 4 commits into
fjordllc:mainfrom
T-REX1996:debug
Open

T-REX1996 wants to merge 4 commits into
fjordllc:mainfrom
T-REX1996:debug

Conversation

@T-REX1996

Copy link
Copy Markdown

概要

cafe.rb に含まれていた3つのバグを修正しました。

修正内容

1. メニュー番号と配列インデックスのズレ

表示上は1始まりの番号((1)コーヒー)だったのに対し、選択した番号をそのまま配列のインデックスとして使っていたため、1つズレたメニューが選ばれてしまっていました。
→ 1始まりの番号をキーにしたハッシュ(numbered_menus)を作ることで、ズレの原因になっていた -1 の計算自体をなくしました。

2. 合計金額計算時の配列の対応ミス

FOODS[order1] + DRINKS[order2] のように、ドリンクの注文番号でフード配列を、フードの注文番号でドリンク配列を参照してしまっていました。
→ take_order の戻り値を「選択されたメニューそのもの(ハッシュ)」に変更し、order1[:price] + order2[:price] のように直感的に計算できるようにしました。

3. 価格が文字列になっていたことによる計算ミス

price: '300' のように価格が文字列で定義されていたため、+ が数値の足し算ではなく文字列の連結として扱われ、合計金額が正しく計算されていませんでした(例: 520460円のような表示)。
→ price: 300 のように数値として定義し直しました。

@coderabbitai

coderabbitai Bot commented Aug 22, 2026 •

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 095651ad-5d57-4aa5-a7cf-aa64b003d2b3


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@T-REX1996 T-REX1996 closed this Aug 22, 2026
@T-REX1996 T-REX1996 reopened this Aug 22, 2026

@yoshitsugu yoshitsugu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

コメントしました!

Comment thread cafe.rb Outdated
Comment on lines +18 to +17
menus.each.with_index(1) do |menu, i|
numbered_menus = menus.each.with_index(1).to_h { |menu, i| [i, menu] }
numbered_menus.each do |i, menu|

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

この修正は特に必要ないように思いますが、どうでしょうか。

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ご指摘の通り不要でしたので、元の実装に戻しました。

Comment thread cafe.rb Outdated
order_number
menu = numbered_menus[order_number]
puts "#{menu[:name]}(#{menu[:price]}円)ですね。"
menu

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

メソッドのインターフェース(入力と出力の形)を変えることは今回はしないようにしてください。
バグ修正が目的なので、あまりおおげさな挙動変更は必要ありません。

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

take_order の戻り値を元の番号に戻し、- 1 で調整する形に修正しました。

Comment thread cafe.rb

puts 'bugカフェへようこそ!ご注文は? 番号でどうぞ'
order1 = take_order(DRINKS)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

今回の目的からすると不要な差分はないようにしてください。

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

②の修正に伴い、この部分の差分もなくなりました。

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

行が削除された差分は残っているようです。

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ご指摘ありがとうございます。
不要な差分を削除しました

@yoshitsugu yoshitsugu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

細かいと思われるかもしれませんが、不要な差分はコードレビューの雑音となりますので、たとえば不要行の削除など、コードフォーマットのリファクタリングを行う場合はバグ修正や機能追加などとは別のPull Request(PR)として、目的毎にPRをわける方法がよくとられます。

Comment thread cafe.rb
puts "#{menus[order_number - 1][:name]}(#{menus[order_number - 1][:price]}円)ですね。"
order_number
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ここも行削除の差分がでています。関係のない差分はできるだけないようにしましょう。

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

こちらも不要な差分を削除しました

Comment thread cafe.rb

puts 'bugカフェへようこそ!ご注文は? 番号でどうぞ'
order1 = take_order(DRINKS)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

行が削除された差分は残っているようです。

Comment thread cafe.rb

puts 'フードメニューはいかがですか?'
order2 = take_order(FOODS)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

こちらもですね。

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

こちらも不要な差分を削除しました

Comment thread cafe.rb
@@ -1,34 +1,28 @@
# frozen_string_literal: true

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ここもですね

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

こちらも不要な差分を削除しました

@yoshitsugu yoshitsugu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

よさそうです!
その上で、今後の事故防止という意味で、リファクタリングに近い修正になりますが、一点だけ考えてみてください。

発展的な話題ですが、そもそも order1 , order2 という変数名が今回の題材となった変数の取り違えのバグを誘発していそうですね。わかりやすいように変数名を変えてみてください。

@T-REX1996

Copy link
Copy Markdown
Author

@yoshitsugu
ご指摘ありがとうございます!
いただいた内容を基に考えて
order1→drink_order_number、order2→food_order_numberに変更し、
変数名を見ただけでどのメニューに対応する注文番号かがわかるように修正しました。
これにより、万が一DRINKS[food_order_number - 1]のように書き間違えても、
変数名とメニューの対応の矛盾に気づきやすくなるかと思います

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