Skip to content

cafe.rbのバグ修正#47

Open
oyamairishitomi wants to merge 5 commits into
fjordllc:mainfrom
oyamairishitomi:fix-cafe-bug
Open

cafe.rbのバグ修正#47
oyamairishitomi wants to merge 5 commits into
fjordllc:mainfrom
oyamairishitomi:fix-cafe-bug

Conversation

@oyamairishitomi

@oyamairishitomi oyamairishitomi commented May 25, 2026

Copy link
Copy Markdown

cafe.rbを修正しました。

Summary by CodeRabbit

  • バグ修正
    • 注文選択時の項目参照の不整合を修正しました。
    • 会計合計金額の計算順序を正しくして精度を改善しました。
  • 雑務(内部改善)
    • メニューの価格表記を文字列から数値に変更し、計算の安定性を向上しました。
    • 開発向けスタイル設定を更新し、共通のルールセットを継承するようにしました。

@coderabbitai

coderabbitai Bot commented May 25, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

cafe.rb のメニュー定義で price を文字列から数値へ変更し、take_order の入力を配列インデックスに合わせ、会計合算の参照順を修正しました。加えて .rubocop.ymlrubocop-fjord の設定継承を追加しました。

Changes

カフェ注文システムの修正

Layer / File(s) Summary
メニュー価格を文字列から数値へ
cafe.rb
DRINKSFOODS の各項目で price リテラルを文字列(例: '300')から整数(例: 300)へ変更します。
注文入力のインデックス調整
cafe.rb
take_order でユーザー入力を gets.to_i - 1 に変更し、1始まりの入力を配列の0始まりインデックスに合わせます。
合算元と合計計算の修正
cafe.rb
total の計算で参照順を DRINKS[drink_order][:price] + FOODS[food_order][:price] にして、数値同士を合算するように修正します.

RuboCop 設定の継承追加

Layer / File(s) Summary
.rubocop.yml に inherit_gem を追加
.rubocop.yml
inherit_gem を用いて rubocop-fjordconfig/rubocop.yml を継承する設定を追加します.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

ご注文は数字で数えるよ、🐇
文字列は数に変えて、
ひとつ引いて索引合わせ、
合計は正しく足し合わせる、
小さなカフェはまた動き出す。

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive PRタイトル「cafe.rbのバグ修正」は、実際の変更内容(DRINKS/FOODSの価格を文字列から数値への変更、take_orderの入力処理修正、totalの計算順序修正、.rubocop.ymlの設定追加)の主な内容を正確に反映していません。タイトルは広すぎて、具体的な修正内容が不明確です。 より具体的で説明的なタイトルに変更してください。例:「DRINKS/FOODSの価格を数値に変更し、注文処理を修正」など、主な変更点を明確に示すタイトルが望ましいです。
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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 and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@cafe.rb`:
- Around line 23-25: The input handling around order_number is unsafe: currently
using gets.to_i - 1 can produce -1 or out-of-range indices and cause
menus[order_number] to be nil; update the input loop around gets.to_i - 1 (the
order_number variable) to validate that the parsed integer is within
1..menus.length, re-prompting the user on invalid, non-numeric, or out-of-range
input, and only return the validated zero-based index; ensure you handle
non-numeric entries (empty/letters) and trim whitespace before parsing so
menus[order_number][:name] / [:price] never accesses nil.
- Line 34: 計算式の配列参照が逆になっているため会計が誤るので、total の算出で FOODS と DRINKS の参照を入れ替えて、order1
を DRINKS、order2 を FOODS のインデックスとして使うよう修正してください(変数: total, FOODS, DRINKS, order1,
order2)。
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4a27b0e2-a2d7-43ca-9c08-af3917cf8be2

📥 Commits

Reviewing files that changed from the base of the PR and between 2051722 and d8c2f96.

📒 Files selected for processing (1)
  • cafe.rb

Comment thread cafe.rb
Comment on lines +23 to 25
order_number = gets.to_i - 1
puts "#{menus[order_number][:name]}(#{menus[order_number][:price]}円)ですね。"
order_number

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

入力値の境界チェックがなく、誤注文やクラッシュを起こします。

gets.to_i - 1 だけだと、0 や非数入力で -1 になり末尾メニューを誤って選択します。さらに範囲外入力時に menus[order_number]nil となり例外化します。入力値を検証して再入力させるべきです。

修正案
 def take_order(menus)
   menus.each.with_index(1) do |menu, i|
     puts "(#{i})#{menu[:name]}: #{menu[:price]}円"
   end
-  print '>'
-  order_number = gets.to_i - 1
-  puts "#{menus[order_number][:name]}(#{menus[order_number][:price]}円)ですね。"
-  order_number
+  order_number = nil
+  loop do
+    print '>'
+    input = gets
+    raise '入力が終了されました' if input.nil?
+    n = input.to_i
+    if n.between?(1, menus.length)
+      order_number = n - 1
+      break
+    end
+    puts "1〜#{menus.length}の番号を入力してください。"
+  end
+  puts "#{menus[order_number][:name]}(#{menus[order_number][:price]}円)ですね。"
+  order_number
 end
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@cafe.rb` around lines 23 - 25, The input handling around order_number is
unsafe: currently using gets.to_i - 1 can produce -1 or out-of-range indices and
cause menus[order_number] to be nil; update the input loop around gets.to_i - 1
(the order_number variable) to validate that the parsed integer is within
1..menus.length, re-prompting the user on invalid, non-numeric, or out-of-range
input, and only return the validated zero-based index; ensure you handle
non-numeric entries (empty/letters) and trim whitespace before parsing so
menus[order_number][:name] / [:price] never accesses nil.

Comment thread cafe.rb Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In @.rubocop.yml:
- Around line 2-4: `.rubocop.yml` currently uses inherit_gem: rubocop-fjord ->
"config/rubocop.yml" but the repo lacks a Gemfile/gemspec so RuboCop may fail to
load that gem; update the project to declare rubocop-fjord as a dependency (e.g.
add rubocop-fjord to the Gemfile or gemspec) or add a clear note in CI
configuration / README that the CI installs the rubocop-fjord gem before running
RuboCop, and ensure the CI job installs the gem (or runs bundle install) so
inherit_gem: rubocop-fjord and the referenced "config/rubocop.yml" can be
resolved at runtime.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e5bdd40b-38d1-41df-a2f6-9f3e823fb525

📥 Commits

Reviewing files that changed from the base of the PR and between b69a88f and 93914bd.

📒 Files selected for processing (2)
  • .rubocop.yml
  • cafe.rb
🚧 Files skipped from review as they are similar to previous changes (1)
  • cafe.rb

Comment thread .rubocop.yml Outdated
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.

1 participant