My code passes the lesson, but it's not doing what it's supposed to - Any assistance is welcome!


#1

Okay. Below, I've pasted the code that pertains to the issue I'm having, but not the entire code, as to make this easier for someone else to help me review. The error I'm getting is in the command line, not from the lesson (obviously, as the lesson is passing). My screen shot shows you what's happening through the execution of my code - if anyone has any thoughts as to how I can modify this, I would love to hear it. I've been playing around with this "passing" code for some time now and can't figure out what exactly it is I'm missing...

...........
..........
.......

case choice
when "add"
puts "What would you like to see?"
title = gets.chomp
if movies[title.to_sym].nil?
else
puts "This movie already exists! It has a rating of #{rating}."
end
puts "How would you rate it, from 1 - 5?"
rating = gets.chomp
movies[title.to_sym]=rating.to_i
puts "Movie has been added with a rating of #{rating}!"
when "update"
puts "What would you like to update?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "This title is not in the list, please try again."
else
puts "How would you rate it, from 1 - 5?"
rating = gets.chomp
movies[title.to_sym]=rating.to_i
puts "#{title} has been updated with a rating of #{rating}!"
.........
.....
............

You will see that the error is happening in the "update" portion, but this is also the case in the "add" section, as the syntax are both the same - I gave up trying to figure it out in the "add" section and just continued, but now that I'm seeing it again, I know there's something wrong and my code is not being properly tested. Hmmmmm... :expressionless:

Thanks in advance for any insight!!


#2

@protrad3r,

I have modified your =add= case....
now it is your turn......

movies = {
    "Conair" => 5,
    "Hitch" => 6
}
choice = "add"
#choice ="update"
case choice 
when "add" 
    puts "What would you like to see?" 
    title = "Conair"
    #title = gets.chomp
    #if movies[title.to_sym].nil?
        # do nothing
    #else
    if movies.has_key?(title)
        rating = movies[title] 
        puts "This movie already exists! It has a rating of #{rating}."
    end 
    puts "How would you rate it, from 1 - 5?"
    rating = "10"
    #rating = gets.chomp
    movies[title]=rating.to_i
    puts "Movie has been added with a rating of #{rating}!"
    puts movies[title]
when "update"
    puts "What would you like to update?"
    title = gets.chomp
    puts
    if movies[title].nil?
        puts "This title is not in the list, please try again."
    else 
       puts "How would you rate it, from 1 - 5?"
       rating = gets.chomp
       movies[title.to_sym]=rating.to_i
       puts "#{title} has been updated with a rating of #{rating}!"
    end
end

Reference
google search
ruby return value of gets.chomp site:stackoverflow.com
http://stackoverflow.com/questions/23193813/how-does-gets-and-gets-chomp-in-ruby-work
ruby how to test for key in hash site:stackoverflow.com
http://stackoverflow.com/questions/4528506/check-if-a-specific-key-is-present-in-a-hash-or-not
http://ruby-doc.org/core-1.9.3/Hash.html#method-i-has_key-3F