Delete! (My code won't work!)


#1

So, here's my code. For some reason it keeps saying that there isn't an "end" on line 30 (between the display prompt and delete) when there is. And before you say to eliminate all of the "end"'s between each "when"'s, I tried and my code won't work. It asks me to put them back in.

So what am I missing? Or doing wrong here?

movies = {
    "Iron Man" => 4
}
puts "what would you like to do?"
choice = gets.chomp.downcase
case choice
when "add" 
puts "What would you like to add?"
title = gets.chomp
title = title.to_sym
if movies[title.to_sym].nil?
puts "What its rating? (enter 1-4)"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "#{title} has been added with a rating of #{rating}"
else puts "#{title} already exists. Its rating is #{movies[title.to_sym]}!" 
end
when "update"
puts "What title would you like to update?"
title = gets.chomp
    if movies[title].nil?
        puts "This 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 { |movie,rating| puts "#{movie}: #{rating}" }
end
when "delete"
puts "Which title would you like to delete?"
title = gets.chomp
if movies[title.to_sym].nil?
    puts "This movie does not exist"
else 
    movies.delete(title.to_sym)
    puts "#{title} has been removed"
end

#2

The end in the above may be removed since you are using a block, rather than a do..end. Try that and report back, please.


#3

It worked only if I added more end's at the end...
Which is strange because I tried that the first time around and it didn't work.


#4

This should be written in symbol form. If you try to add 'Iron Man' it will let you since it doesn't find the symbol in your hash.

    :'Iron Man' => 4

#5

Which exercise should we test your code in? Please post a link. Thank you.