FAQ: A Night at the Movies - Delete

This community-built FAQ covers the “Delete” exercise from the lesson “A Night at the Movies”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise Delete

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 (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 (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

If I add a line to

puts movies

after the

puts movies.delete(title)

the console print out does not show that any movie has been deleted(?)

Is this a bug or is this something to do with the KVP immutability?

(I’ve made sure to test this using the solution code provided)

1 Like

Hello :slight_smile: Welcome to the forum!

Unfortunately, this is a bug in the solution code.

In hashes, each named key is a symbol. So to delete the given movie you have to turn a string into a symbol. Do this:

movies.delete(title.to_sym)

instead of:

puts movies.delete(title) # put does nothing

Thank you for bringing this to our attention, I will report this bug. Great catch! :slight_smile:

2 Likes

The example provided movies.delete([title.to_sym]) appears to be wrong and should be movies.delete(title.to_sym) (with no brackets).

2 Likes

Yes, this was also reported. We have to simply wait for the fix :slight_smile:

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?

This is my code, for reference:

image

seems it’s already solved, nice :slight_smile:

Good question, would love to hear the answer to this even though its 7 months later.

delete() is an Hash class method which deletes the key-value pair :slight_smile:

1 Like

It just keeps on loading forever. Usually refreshing the page fixes it but not on this one.

1 Like