映画のタイトルとそれに関連付けられている評価を更新して、既存のハッシュに追加する必要があります。これは私がこれまでに持っているコードです:
movies = {
Titanic:4,
Bestman: 3,
Agora: 2
}
puts "What would you like to do?"
choice = gets.chomp
case movies
when "add"
puts "What movie do you want to add?"
title = gets.chomp
puts "What's the rating of the movie?"
rating = gets.chomp
movies[title] = rating
puts "#{title} has been added with a rating of #{rating}."
when "update"
puts "Updated!"
when "display"
puts "Movies!"
when "delete"
puts "Deleted!"
else
puts "Error!"
end
コードを実行すると、「ムービー ハッシュに追加しなかったようです」というエラーが表示されます。
エラーが次の行の間のどこかにあることはわかっています。
case movies
when "add"
puts "What movie do you want to add?"
title = gets.chomp
puts "What's the rating of the movie?"
rating = gets.chomp
movies[title] = rating
puts "#{title} has been added with a rating of #{rating}."
私はそれを理解しようとしてきましたが、これまでのところ、私が間違っていることを理解できていません。
映画のハッシュに追加する別の方法はありますか? また、映画のタイトルと評価が追加されたことをユーザーに知らせるための puts コードの何が問題になっていますか?
ありがとうございました
EDIT Some Guy が指摘したように、case ステートメントを
case movies
に
case choice
問題を解決しました。
私が学ぶ/理解する必要があるのは、2番目が機能するのに1番目が機能しない理由です。