Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions cafe.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
# frozen_string_literal: true

DRINKS = [
{ name: 'コーヒー', price: '300' },
{ name: 'カフェラテ', price: '400' },
{ name: 'チャイ', price: '460' },
{ name: 'エスプレッソ', price: '340' },
{ name: '緑茶', price: '450' }
{ name: 'コーヒー', price: 300 },
{ name: 'カフェラテ', price: 400 },
{ name: 'チャイ', price: 460 },
{ name: 'エスプレッソ', price: 340 },
{ name: '緑茶', price: 450 }
].freeze

FOODS = [
{ name: 'チーズケーキ', price: '470' },
{ name: 'アップルパイ', price: '520' },
{ name: 'ホットサンド', price: '410' }
{ name: 'チーズケーキ', price: 470 },
{ name: 'アップルパイ', price: 520 },
{ name: 'ホットサンド', price: 410 }
].freeze

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
order_number = gets.to_i - 1
puts "#{menus[order_number][:name]}(#{menus[order_number][:price]}円)ですね。"
order_number
end
Expand All @@ -30,5 +30,5 @@ def take_order(menus)
puts 'フードメニューはいかがですか?'
order2 = take_order(FOODS)

total = FOODS[order1][:price] + DRINKS[order2][:price]
total = FOODS[order2][:price] + DRINKS[order1][:price]
Copy link

Choose a reason for hiding this comment

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

動作的には問題ないのですけど、本質的にはorder1order2というわかりにくい命名をしていることがこの種の不具合を生む原因になったりします。従ってよりよい回答を目指すのならこのわかりにくい変数名をわかりやすくするのが望ましいです。

Copy link
Author

Choose a reason for hiding this comment

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

ロジックのエラーばかり気にしていて変数の不明瞭さは盲点でした。
今後気をつけていきたいと思います!

puts "お会計は#{total}円になります。ありがとうございました!"