There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
hello i don’t understand something
what does it mean “Make sure to test your program by choosing ‘delete’”
my code is correct but i can’t pass this step because of that
thank you for helping me
So when I run this code, in the web console it asks what I want to do. When I type delete it skips over the title = gets.chomp code and tells me the name does not exist.
Any suggestions?
movies = {
StarWars: 4.8,
Divergent: 4.7
}
puts "What would you like to do? "
choice = gets.chomp
case choice
when "add"
puts "What movie would you like to add? "
title = gets.chomp
if movies[title.to_sym].nil?
puts "What rating does the movie have? "
rating = gets.chomp
movies[title.to_sym] = rating.to_i
else
puts "That movie already exists! Its rating is #{movies[title.to_sym]}."
end
when "update"
puts "What movie would you like to update? "
title = gets.chomp
if movies[title.to_sym].nil?
puts "That movie does not exist."
else
puts "What is the new rating? "
rating = gets.chomp
movies[title.to_sym] = rating.to_i
end
when "display"
movies.each do |title, rating|
puts "#{title}: #{rating}"
end
when "delete"
puts "Which movie would you like to delete?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "That movie does not exist."
elsif
movies.delete(title.to_sym)
puts "#{title} has been deleted."
else
puts "Error!"
end
end
Does anyone know why we don’t use movies[title.to_sym] = rating.to_i in the “delete” part of the code? I thought the purpose of having movies[title.to_sym] = rating.to_i in our code was to convert whatever answer the user typed in to a symbol and integer, respectively. How does Ruby know to delete a title if movies[title.to_sym] = rating.to_i is not part of the “delete” section? Is this because the hash has already been saved? Or is it because movies[title.to_sym] is already included in the code? Doesn’t the integer have to be manually deleted alongside the symbol? Or does it get automatically deleted once we delete the symbol?