6 Error Error


#1


I dont understand when I put a movie that already exists I get mesage from 'if' like the movie dont exist.

movies = {
    "Snach" => 5,
    "star wars" => 5,
}
puts "What do you wist to do (add,)?"
choice = gets.chomp
case choice
when "add"
    puts "What movie title you want to add?"
    title = gets.chomp
    if movies[title.to_sym].nil?
        puts "What rating you give for this movie? (0 - 5)"
        rating = gets.chomp
        movies[title.to_sym] = rating.to_i
        puts "Movie #{title} with a rating #{rating} is added to list"
    else
        puts "Movie already exists in list"
    end    
when "update"
puts "Update!"
when "display"
puts "Movies!"
when "delete"
puts "Deleted!"
else
    puts "Error!"
end


#2

Remove the last comma in the movies hash.


#3

I done that but still is the same problem


#4

Should be:

choice = case


#5

No, this isn't true. Its

case choice

even in the initial example. In fact choice = case returns an error.


#6

Did you figure this out? I'm having the same problem


#7

Haha... Okayy!

Do me a favour, please post your code and error! :slight_smile:


#8

Also, as to looking at the initial, as a mirror, you're not wrong but what you will be wrong about is that, this is a set of exercises to BUILD the initial and so...

Please post your well formatted code so I see why my suggested code might not be working for you. :slight_smile:


#9

The reason why is not working to you, it is because there is a mistake in the example.

if movie[title.to_sym] . nil =? (dot)

When it should be

if movie[title.to_sym] = nil =? (equal)

...
What we are doing is evaluating the "new title" in the current movie hash.
If the "new title" already exist, them it would have a value associated, which means the result will be different of nil.
If it is a new key, when we evaluate it the result would be nil and we can continue adding the new entrance.

Regards
Eduardo


#10

but code that asks if it is nil is "nil_variable.nil?" with a dot. It literally says so in the tutorial.
one = sign means assign, where == mean compare. At least that is what i learned so far, correct me if i am wrong :slight_smile:

As for the answer. It is because tutorial confused us all XD

Initially it told us to create a hash and gave us the old way as an example --- "key" => Value
But, now it asked us to compare it to a SYMBOL (variable.to_sym)

So the solution is to correctly make the hash on the start with semicolons (" : ")

movies = {
Snach: 5,
star wars: 5
}

Now it compares symbols to symbols. Also remember not to use spaces with symbols :wink:

Or, could compare like so if movies[title].nil? and make it a symbol after that :wink: