Not saving movie titles/ratings to my movies hash


#1



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


I add a movie to the hash, say "Thor", with a rating of 4. I print out my hash to the console and it shows up in my hash. Then I run the program again, and choose "update". I type "Thor" and it says, "Movie Not found" What is the problem here? It's like it is not storing the movie permanently in my hash. I've tried running the example code on my own Mac terminal and I get the same problem. Help me understand why my movie is not being saved in memory, or my program, is it dumping the memory every time I re-run the program? How do I fix this so it will save the movie permanently?


movies = {
  Memento: 3,
  Primer: 4,
  Ishtar: 1
}

puts "What would you like to do?"
puts "-- Type 'add' to add a movie."
puts "-- Type 'update' to update a movie."
puts "-- Type 'display' to display all movies."
puts "-- Type 'delete' to delete a movie."

choice = gets.chomp.downcase
case choice
when 'add'
  puts "What movie do you want to add?"
  title = gets.chomp
  title = title.to_sym
  puts title
  if movies[title].nil?
    puts "What's the rating? (Type a number 0 to 4.)"
    rating = gets.chomp
    rating = rating.to_i
    #movies[title] = rating.to_i
    movies.store(title, rating)
    puts "#{title} has been added with a rating of #{rating}."
  else
    puts "That movie already exists! Its rating is #{movies[title.to_sym]}."
  end
when 'update'
  puts "What movie do you want to update?"
  title = gets.chomp
  title = title.to_sym
  if movies[title].nil?
    puts "Movie not found!"
  else
    puts "What's the new rating? (Type a number 0 to 4.)"
    rating = gets.chomp
    movies[title] = rating.to_i
    puts "#{title} has been updated with new rating of #{rating}."
  end
when 'display'
  movies.each do |movie, rating|
    puts "#{movie}: #{rating}"
  end
when 'delete'
  puts "What movie do you want to delete?"
  title = gets.chomp
  if movies[title.to_sym].nil?
    puts "Movie not found!"
  else
    movies.delete(title.to_sym)
    puts "#{title} has been removed."
  end
else
  puts "Sorry, I didn't understand you."
end


#2

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