10. A night at the movies - unexpected result


#1



Hi

I've passed this course, however, when I test out what happens when I delete a movie title, it does not give me the response that I expect. I tried to delete a movie title that SHOULD be there, but it tells me it does not exist. Why is that?

Thanks


https://www.codecademy.com/courses/ruby-beginner-en-NFCZ7/2/1?curriculum_id=5059f8619189a5000201fbcb

https://www.codecademy.com/en/courses/ruby-beginner-en-0i8v1/0/10?curriculum_id=5059f8619189a5000201fbcb


This is the response I get back:

What do you want to do? Type 'add', 'update', 'display' or 'delete' ?
delete
What is the title you want to delete?
UHF
The title is not in the list. Nothing to delete
nil


movies = {
    'UHF' => 9
}
puts "What do you want to do? Type 'add', 'update', 'display' or 'delete' ? "
choice = gets.chomp.downcase

case choice
    when "add"
    puts "What is the title of the movie?"
    title = gets.chomp    
    puts "What would you rate this movie out of 10?"
    rating = gets.chomp
 
    if movies[title.to_sym] = nil
        puts "Movie already exists!"
    else
        movies[title.to_sym] = rating.to_i
    end

    when "update"
    puts "What is the updated title of the movie?"
    title = gets.chomp
    if movies[title.to_sym].nil?
        puts "Errrorrr!"
    else
        puts "What would you rate this movie out of 10?"
        rating = gets.chomp
        movies[title.to_sym] = rating.to_i
    end
    
    when "display"
    movies.each do |movie,rating| 
        puts "#{movie}: #{rating}"
    end
    
    when "delete"
    puts "What is the title you want to delete?"
    title = gets.chomp
    if movies[title.to_sym].nil?
        puts "The title is not in the list. Nothing to delete"
    else movies.delete(title.to_sym)
    end
    
else
    puts "Error! Please enter a valid command."
end


#2

We would only be able to compare to nil if a value is already set to it. In that case we would use a comparison operator... ==.

The thing we are testing for, (non-existence in the keys) can be determined with,

if movies[title.to_sym].nil?

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.