Delete


#1




undefined local variable or method `movie' for #


movies = { 
    'Kingglaive' => 5
}
puts "Do you like that movie?"
choice = gets.chomp
case choice
when "add"
  puts "Enter the name of the movie!"
  title = gets.chomp
  puts "What's your rating about it?"
  rating = gets.chomp
  movies[title.intern] = rating.to_i
  if movies[title.intern].nil? 
  puts "#{rating} + movies[title]"
  else 
      puts "It already exist!"
  end
when "update"
  puts "What's your movie in mind?"
  title = gets.chomp
  if movie[title].nil?
      puts "You're wrong!"
  else
      puts "What's your new rating then?"
      new_rating = gets.chomp
       movies[title.intern] = rating.to_i
       puts "#{title} has been updated with your new #{rating}"
   end
when "display"
  movies.each do |movie, rating| puts "#{movie}: #{rating}"
  end
when "delete"
  puts "What title do you want to delete?"
  title = gets.chomp.to_sym
  movie.delete[title.intern]
    if movie[title.intern.to_s].nil?
      puts "Error!"
    else
      puts "The movie #{title} with a rating of #{rating} has now been deleted!"
end
end


#2

The movie hash is not defined. As a side note, you could demonstrate a little more maturity in your puts statement. Try using a response to the user that actually tells them something.

puts "The movie #{movie} does not exist in the database."

Again, movie is not defined.

While intern may be the logical equivalent of .to_sym, it is more common to see it used in an assignment:

new_title = title.intern

Typical in-place conversion uses string.to_sym. But this is not a sticking point, as long as you are sure your approach is working.

On the whole, this is quite messy and convoluted when we look at it.

gets.chomp.to_sym

I wouldn't advise doing it this way. Leave the inputs alone and modify later.

Why convert to a symbol only to convert it back to a string?


#3

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