Errors with Error! Error!


#1

Hey guys. Super Noob here. First time poster.
I found a few on this already but still no luck. Every time to I enter a movie that is already listed in the hash, it doesn't recognize it already being there. Any thoughts?

movies = {
"Forest Gump" => 6,
"Django Unchaine" => 8,
"Saving Private Ryan" => 9,
"Notebook" => 3
}
puts "Which movie do you want to see?"
puts "Type add to add movies to our list."
puts "Type update to update our list."
puts "Type display to see our list."
puts "Type delete to delete."
choice = gets.chomp

case choice
when "add"
puts "What movie is your favorite?"
title = gets.chomp.capitalize!

if movies[title.to_sym].nil?
    puts "Please enter in your rating"
    rating = gets.chomp
    movies[title.to_sym] = rating.to_i
    puts "#{movies[title]} and your rating of #{movies[rating]} has been entered to the database.  Thank you"
else
    puts "#{movies[title]} already exists!  Try another"
end

when "update"
puts "Updated!"
when "display"
puts "Displayed!"
when "delete"
puts "Deleted!"
else
puts "Error!!!"
end

THANKS!


#2

Either you're testing for a different key or you're not testing whether the key is there.
How can you test that? I suggest creating a minimal piece of code for experimentation purposes

my_hash = {
  :hello => 3
}

puts my_hash.has_key? :hello    # true

#3

Don't do title = gets.chomp.capitalize!. because Ruby doesn't recognize uppercase symbols.
Also, replace

with "#{title.capitalize}" because what you wrote will only show the value of the key. Replace

with "#{movies[title.to_sym].to_s} so that you can find the rating of the movie and turn it into a usable string (I'm not sure if you need the .to_s).


#5

Hi, the problema is that the movies you use to star the Hash Forest Gump Notebook are not symbols, so when ruby compares them to see if the already exists when you type the title they get converted into symbols and thats why it doe not see that they are the same.