以下に示すハッシュを作成する方法を読みました。既に存在するムービーを追加しようとすると、ruby が正しい文字列を返しません。movies
通常のハッシュ (例: = )を入れると、{Pirates: 3, James_Bond: 4}
正しく返されます。これが事実である理由を誰か説明できますか?
movie = %w(Pirates James_Bond Finding_Nemo)
rating = [4, 3, 5]
movies = Hash[movie.zip(rating)]
puts "What would you like to do?"
choice = gets.chomp
case choice
when "add"
puts "What title would you like to add?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "What is the rating of this movie?"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
else
puts "Movie already exists."
end
end