6. Error Error: error message not showing


#1

Hi. I just typed out the code for prompting an error in case the user is adding an already existing movie in the hash. Following is my code:


when "add"
puts "whats the movie you wanna add?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "how much rating do u wanna give?"
rating = gets.chomp
movies[title.intern] = rating.to_i
puts "your entry #{title} has been added with a rating of #{rating}"
else
puts "sorry that movie already exists"
end


This code fails to produce an error. I compared it to the final solution as well. Any reasons why? Please help. Thanks.


#2

Got it!

Removed the .to_sym from line 4 to make it look like this:


if movies[title].nil?


Still need the theoretical explanation though. Can anyone help? What was the issue here?

P.S. Im a newbie at coding. I struggle and screw around with code a lot to get things in a working shape.. Is that approach fine? It doesn't feel right though...


#3

Experimenting is in my [opinion], the best way to fully understand the in's and out's of coding. I do not necessarily do so though. Thumbs up!
-I used .to_sym and it displayed my error, what exactly were you trying to get?


#4

If I remember correctly, there's an exercise stating in the instructions not to use to_sym and if you do you can't pass the exercise. Don't know if it's this one.


#5

I ran into the same issue. The code does not work when .to_sym is used. Removing it, makes the error work, as expected.
In fact, the instructions in step 6 and 7 don't match.
Step 6 states: "If the movie isn't already in the hash (that is, if movies[title.to_sym] is nil)".
Step 7 states: the instructions state "if the movies[title] is nil, then the movie is not in the hash".