Delete OK > Puts not OK


#1



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

I'm a bit confused about why this happens...

My movies are as follows...

movies = { Beaches: 5,
King_Kong: 4,
Hurricane: 3
}

And the section of code concerning deletion of a movie from the Hash is:

when "delete"
puts "Which movie would you like to delete?"
title = gets.chomp
movies [title.to_sym] = rating.to_i
if movies [title.to_sym].nil?
puts "There is no movie called #{title} to delete!"
else
puts "The movie #{title} with rating: #{rating} has now been deleted."
puts movies
end
end

When running the programme, I choose to delete King Kong from the hash.
Then the programme returns a message saying King Kong has been deleted.
But when I ask Ruby to puts movies, I can still see King Kong in the output it returns.
Why does this happen?

Thank you.


movies = { Beaches: 5,
King_Kong: 4,
Hurricane: 3
}

#Section of code that relates to my question:

when "delete" 
    puts "Which movie would you like to delete?"
    title = gets.chomp
    movies [title.to_sym] = rating.to_i
    if movies [title.to_sym].nil?
        puts "There is no movie called #{title} to delete!"
else 
    puts "The movie #{title} with rating: #{rating} has now been deleted."
    puts movies
end
end


#2

If it only says it deletes it, but doesn't, then you'll need to fix or add the code for deleting right?


#3

I would think so :slight_smile: ... the question is: where did I go wrong? :sweat:

Another hint please!


#4

Well I'm gonna say the same thing again pretty much

To delete something, you have to have code that says to do so. Either you have it but it's broken, or you don't and you need to add it.

You've already made the observation that it says it deleted, but did not. That says a whole lot about what needs to be different. Your code should delete it, not just say it.


#5

you never set it to actually delete the movie would need to do movies.delete(title.to_sym). but these changes will not actually stay with the code. because you are not working with a database. if your were to set up a loop to run program until typed end and assign case of

when 'end'
break

you would then beable to see your code affecting the database but the minute you go to next exercise it would start back over with the original movies listed.