5. Problem with adding .to_sym and .to_i


#1

Here is my current code
movies = {
"Jackass" => 3
}
puts "Put in anything because codeacademy didn't give me something to write"
choice = gets.chomp
case choice
when "add"
puts "What's the title of your movie?"
title = gets.chomp
puts "What's the rating for your movie?"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
movies = {}
movies[title] = rating
when "update"
puts "Updated!"
when "display"
puts "Movies!"
when "delete"
puts "Deleted!"
else
puts "Error!"
end
It seems right to me but I keep getting this error:

Oops, try again. Did you call .to_sym on title and .to_i on rating before adding them to the hash?

I also tried
title.to_sym
rating.to_i
but that didn't work either.

It is 4am and my mind could be blanking at the moment but I don't know what I need here.


#2

Hey

After you have added the new title with rating (which is done correctly with the .to_sym and .to_i usage), you recreate the movies variable as a new hash (movies = {}), and then try to assign the values again without converting to symbol and integer.

If you remove those two last lines before the "update" part, it should run correctly


#3

Thank you, that worked.

Now that I look at it, I should've noticed that.


#4

Here's what I did:

movies = {
"First Blood" => 3,
"Undercover Brother" => 4,
"Django Unchained" => 4,
}

puts "Type 'add' to add movie."
puts "Type 'update' to update."
puts "Type 'display' to display movies."
puts "Type delete to remove movies."

choice = gets.chomp

case choice
when "add"
puts "Type in A New Movie Name"
title = gets.chomp.to_sym
puts "What rating will you give your movie?"
rating = gets.chomp.to_i

if movies[title].nil?
movies[title] = rating
puts movies
puts "Your new movie and its rating have been added!"
end

when "update"
puts "Updated!"
when "display"
puts "Movies!"
when "delete"
puts "Deleted!"
else
puts "Error!"
end

Hope it helps. It worked for me.