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
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
This is very strange. Why are you checking nil in the movies[title]
just to do the same assignment again?
when "update"
puts "Please enter the title of the movie :"
title = gets.to_sym
if movies.key?(title) == true
no need to do == true.
puts "Please enter the new rating: "
rating = gets.to_i
movies[title] = rating
else
puts "Key not found!"
end
Hope this helps,
Jesus.
···
On Mon, Sep 9, 2013 at 11:04 AM, Red Rev <lists@ruby-forum.com> wrote:
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