Display is not working


#1

Would someone please tell me what is wrong with my code?

movies = { :Skyfall => 4 }
puts "What would you like to do?"
puts "-- Type 'add' to add a movie."
puts "-- Type 'update' to update a movie."
puts "-- Type 'display' to display all movies."
puts "-- Type 'delete' to delete a movie."
choice = gets.chomp.downcase!
case choice
when "add"
puts "Please type the title of the movie you wish to add."
title = gets.chomp
movies[title.to_sym] = title.to_sym
puts "What is the rating of the movie?"
rating = gets.chomp
movies[ratings.to_i] = ratings.to_i
if movies[title.to_sym].nil?
puts "That movie already exists! Its rating is #{movies[title.to_sym]}."
else
puts "#{title} has been added with a rating of #{rating}."
end
when "update"
puts "Please type the title of the movie you wish to update."
title = gets.chomp
if movies[title.to_sym].nil?
puts "That movie is not currently stored."
else
puts "What is the rating of the movie?"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "#{title} has been added with a rating of #{rating}."
end
when "display"
movies.each do |movie, rating|
puts "#{movie}: #{rating}"
end
when "delete"
puts "Deleted!"
else
puts "Error!"
end


#2

It only works for me if I do "Display" (actually, all the commands on this code only work for me if I capitalize a letter or more in the code) - I'd recommend removing the .downcase! - after that it works fine with me, as long as nothing is capitalized, hehe

You actually helped me with my code looking at that, too, because I made a stupid mistake by not putting my puts in quotes and I was wondering why it wasn't printing. :slight_smile:


#3

Hey

You're using the self-modifying version of downcase (downcase!).
Now, String.downcase will always return a new string which is the lowercase version of the original (documented here), while String.downcase! will modify the original string and return either a lowercase string if anything was changed, or 'nil' if nothing was changed (documented here).

That's why, as codesolver11935 points out, using "Display" (or any other capitalized letters) will work, and not "display", since the first would return a lowercase string while the second would return 'nil'

To fix this, you only need to remove the "!" sign and it should work fine, or you could assign choice the value of gets.chomp, and then run "choice.downcase!" to self-modify the variable.


#4

my code is not working: here it is:movies = {
Terminator: "good"
}

puts "Please enter a favourite movie:"

choice = gets.chomp

case choice
when "add"
puts "Title please : "
title = gets.chomp
t = title.tosym
puts "Rating movie:"
rating = gets.chomp
r = rating.toi
movies[t] = r
puts "The movie has been added"
when "update"
puts "Updated!"
when "display"
puts "Movies!"
when "delete"
puts "Deleted!"
else
puts "Error!"
end