6. Error!Error! error message wants me to add existing movie to hash?


#1

Hi,
in my movies hash there is one film "Le Prenom". When I run my code and try to add this already existing movie, it does give me the right output i.e. "This movie already exists", but I still get an error message stating:
"Oops, try again. It looks like Le Prenom wasn't added to the movies hash with a rating of ."

To me it seems that of course, the movie shouldn't be added to the movie hash, as it is already in there... so what is the problem?

Anyone got any ideas?

Here is my code:
^^^
movies = {
"Le Prenom" => 5
}

puts "What would you like to do?"
puts "Please type ADD to add a movie."
puts "please type UPDATE to change a movie's rating."
puts "please type DISPLAY to display a list of all rated movies."
puts "please type DELETE to delete a movie from the list."
choice = gets.chomp.downcase
case choice
when "add"
puts "What is the title of the movie you wish to add?"
title = gets.chomp
if movies[title] == nil
puts "What is your rating of this movie on a scale from 0-5, 0 being horrendously awful and 5 being a magnificent piece of cinematic craft?"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "#{movies.keys.last} was added successfully."
else
puts "This movie already exists."
end

^^^


#2

Hey

It seems that the movies hash you create from start, has the key type of string.
You can verify this by running the command "puts movies.keys.first.class", which will output "String"

By adding the colon sign in front of "Le Prenom", it will be added as a symbol into the hash, and the command above will output "Symbol" instead

After that, I had to tweak your script a bit, so that it would run "if movies[title.to_sym] == nil" to compare symbol to symbol, instead of symbol to string