Hi.
I’m new to ruby and to programming too.I have a task which i need to
complete.
I have problems with the update part of the code. I need to check if the
key given by the user exists in the hash and if yes i need to update the
rating of that movie. So far the code can’t find the key and print’s out
the "Key not found " string.
puts "Please enter your input: "
choice = gets.chomp
case choice
when “add”
puts "Please enter the title of the movie: "
title = gets.to_sym
puts "Please enter the rating of the movie: "
rating = gets.to_i
movies[title] = rating
if movies[title] == nil
movies[title] = rating
else
puts “The movie #{title} was successfully added.”
end
when “update”
puts “Please enter the title of the movie :”
title = gets.to_sym
if movies.key?(title) == true
puts "Please enter the new rating: "
rating = gets.to_i
movies[title] = rating
else
puts “Key not found!”
end
PS: its not an good idea to use to_sym on user input on maybe longer
running objects, because this symbols are not deleted from the GC so you
may fill your RAM with it
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.