Preview shows correct result but I'm not getting green


#1



https://www.codecademy.com/courses/ruby-beginner-en-0i8v1/0/4?curriculum_id=5059f8619189a5000201fbcb#


Trying to add new movie in a hash. Preview says that movie has been added but at the same time I'm getting error saying "Opps .... looks like you didn't add xxx into your hash"


movies = {
    Hancock: 1,
    The_Social_Network: 2,
    The_Source_Code: 3,
    Transformers: 4
}


puts "What you wanna do with your movie library?"
choice = gets.chomp
case choice
    when "add"
        puts "Name of movie ?"
        title = gets.chomp
        if movies[title.to_sym].nil?
        puts "What's the rating (type number 0 - 4) ?"
        rating = gets.chomp
        movies[title.to_sym] = rating.to_i
    puts "#{title} has been added with a rating of #{rating}."
        else
            puts "That movie already exists in your library!"
        end


#2

@markiscoding
You are missing 1 =end= statement with which you close the =case= statement
The structure should look like

case
  when
    if
       # 
    else
       #
    end
end

Referece:
google search
== discussions / opinions ==
ruby case when site:stackoverflow.com
http://stackoverflow.com/questions/5114584/how-to-use-ruby-case-when-with-inequalities


#3

I added one more end but program didn't even start to run. Can't be that.


#4

Also I can peek to original code made by Codecademy and they don't have 2 ends. But for them the code works and when I use the same code it doesn't work for me. ■■■■ .....


#5


#6

@markiscoding

A normal CASE-WHEN-ELSE structure

case
  when
       if
           # 
       else
           #
        end
  else
       # an ELSE for the case
end

But the cause for your problem
lies in the fact that you are using the to_sym and to_i methods.....!!!!
Remove them from your code .....
and i got a pass using a windows 8.1 IE11 environment....


#7

Will try ... thank you ...


#8

The error message is suggesting that Superman missing in your.. whatever-it's-called-in-ruby (hash table/dictionary) so if you compare how/what you added to what they say to be added then you'll spot that instructions say that you "don't need to" use to_sym although they seem to mean "you may not"


#9

I got green on this code although it says "nil can't be coerced into fixnum" in preview whatever that means ....
Thank you Leo for your input ... you're always on hand and I appreciate that !

movies = {
Hancock: 1,
The_Social_Network: 2,
The_Source_Code: 3,
Transformers: 4
}

puts "What you wanna do with your movie library?"
choice = gets.chomp
case choice
when "add"
puts "Name of movie ?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "What's the rating (type number 0 - 4) ?"
rating = gets.chomp
movies = {}
movies[title] = title
puts 4 + movies["title"]
puts "#{title} has been added with a rating of #{rating}."
else
puts "That movie already exists in your library!"
end

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

#10

If it crashed then you definitely didn't get it right.


#11

@markiscoding,
Remove the to_sym