cafe.rbのバグ修正#47
Conversation
Walkthrough
Changesカフェ注文システムの修正
RuboCop 設定の継承追加
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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
| order_number = gets.to_i - 1 | ||
| puts "#{menus[order_number][:name]}(#{menus[order_number][:price]}円)ですね。" | ||
| order_number |
There was a problem hiding this comment.
入力値の境界チェックがなく、誤注文やクラッシュを起こします。
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.
There was a problem hiding this comment.
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
📒 Files selected for processing (2)
.rubocop.ymlcafe.rb
🚧 Files skipped from review as they are similar to previous changes (1)
- cafe.rb
cafe.rbを修正しました。
Summary by CodeRabbit