9 (undefined method `to_sym' for nil:NilClass)


#1

Hi everyone, I have been stuck on this exercise for days.

This is the link of the exercise I am stuck at.

The error "undefined method `to_sym' for nil:NilClass" keeps prompting to me. Can someone help me check my code? Thanks!

movies = { 
    Transformers: 5,
    Avatar: 9,
    Gone_Girl: 7,
    Euro_Trip: 8,
    Thats_My_Boy:5,
}

puts "What do you want to do?"
puts ">> Type 'add' to add a new movie!"
puts ">> Type 'update' to update a current movie ranking!"
puts ">> Type 'display' to showcase all movies!"
puts ">> Type 'delete' to delete a movie!"
choice = gets.chomp.downcase

case choice
when 'add'
    puts "Please input a movie of your choice."
    title = gets.chomp.to_sym
    puts "Please give me the rating on a scale of 1 to 10."
    rating = gets.chomp.to_i
    if movies[title].nil?
    movies= {}
    movies[title.to_sym]=rating.to_i
    puts "The movie #{movie} has a rating of #{rating}!"
    else
    puts "The movie already exists!"
    end
when 'update'
    puts "Please give us the movie to update."
    gets.chomp.to_sym
    if movies[title.to_sym].nil?
        puts "Error! The movie is not found!"
    else
        puts "Give us the new rating for the movie."
        gets.chomp.to_i
        puts "The movie has a new rating of #{movies[rating]}"
    end
    movies {}
    movies[title.to_sym]=rating.to_i
when 'display'
    movies.each do |movie, rating|
        puts "#{movie}: #{rating}"
    end
when 'delete'
    if movies[title.to_sym].nil?
    puts "Sorry your movie is not found!"
    else
    movies.delete(title)
    puts "Deleted!"
    end
end

#2

This line needs to be removed. It is wiping out the movies hash.


#3

Hi mtf. Thank you for your reply, but the error "undefined method to_sym' for nil:NilClass" still prompts after deleting

movies={}

Any other ideas?


#4

This implies an error on the inputs. Rather than getting a string, it gets nil which may be a return from a method that is supposed to change an input, but changes nothing. Re-post your current working code and let's have a closer look.


#5

Here it is, thanks for looking at it.

movies = { 
    Transformers: 5,
    Avatar: 9,
    Gone_Girl: 7,
    Euro_Trip: 8,
    Thats_My_Boy:5,
}

puts "What do you want to do?"
puts ">> Type 'add' to add a new movie!"
puts ">> Type 'update' to update a current movie ranking!"
puts ">> Type 'display' to showcase all movies!"
puts ">> Type 'delete' to delete a movie!"
choice = gets.chomp.downcase

case choice
when 'add'
    puts "Please input a movie of your choice."
    title = gets.chomp.to_sym
    puts "Please give me the rating on a scale of 1 to 10."
    rating = gets.chomp.to_i
    if movies[title.to_sym].nil?
    movies[title.to_sym]=rating.to_i
    puts "The movie #{movie} has a rating of #{rating}!"
    else
    puts "The movie already exists!"
    end
when 'update'
    puts "Please give us the movie to update."
    gets.chomp.to_sym
    if movies[title.to_sym].nil?
        puts "Error! The movie is not found!"
    else
        puts "Give us the new rating for the movie."
        gets.chomp.to_i
        puts "The movie has a new rating of #{movies[rating]}"
    end
movies[title.to_sym]=rating.to_i
when 'display'
    movies.each do |movie, rating|
        puts "#{movie}: #{rating}"
    end
when 'delete'
    if movies[title.to_sym].nil?
    puts "Sorry your movie is not found!"
    else
    movies.delete(title.to_sym)
    puts "Deleted!"
    end
end

#6

This is the sort of thing we're looking for, just to clarify...

 > choice = gets.chomp.downcase
  ▒
=> ""
 > choice
=> ""
 > choice = gets.chomp.downcase!
  ▒
=> nil

In both instances I hit Enter with no other inputs.


#7

Haha I'm sorry but I don't think I understand :frowning:


#8

What sort of stuff are you doing with gets.chomp that might be messing with the inputs? I see at least four lines with questionable inputs.


#9

Could it be the [title.to_sym] in the following snippet found throughout the code that is contributing to this error? I replaced all 'title.to_sym' to 'title' and it looks fine now.

when 'delete'
    if movies[title.to_sym].nil?
    puts "Sorry your movie is not found!"
    else

#10

@mtf I think I might have figured out what was wrong! I was using the wrong code. It should have been

[title.to_s].nil?

instead of

[title.to_sym].nil?

I don't think there is 'to.sym' in Ruby syntax. Is this the cause of my problem?


#11

That is not an issue once the others are cleared up.


#12

Actually there is, and you are using it correctly. The issues are these lines:

This line should work with just the title, but may not, so test both ways.


#13

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.