Codeacademy-电影之夜(红宝石)。使用户能够在不区分大小写的情况下查找电影

时间:2019-02-07 14:53:03

标签: ruby

我刚刚完成了Ruby CodeAcademy任务,“电影之夜”(下面的代码)。

在数据库中搜索现有电影时,我希望用户能够输入标题(在这种情况下仅是StarWars或Divergent)而不必区分大小写?目前,搜索“ starwars”会返回错误(与之相反的是StarWars)!

谢谢

movies = {
  StarWars: 4.8, 
  Divergent: 4.7
  }

puts "What would you like to do? "

choice = gets.chomp

case choice
when "add"
  puts "What movie would you like to add? "
  title = gets.chomp.downcase
  if movies[title.to_sym].nil? 
    puts "What rating does the movie have? "
    rating = gets.chomp
    movies[title.to_sym] = rating.to_i
  else
    puts "That movie already exists! Its rating is #{movies[title.to_sym]}."
  end

when "update"
  puts "Please enter movie title."
  title = gets.chomp.to_sym
  if movies[title].nil?
    puts "This title doesn't exist!"
  else 
    puts "what's the new rating for this movie (0 to 4)"
    rating = gets.chomp
    movies[title.to_sym] = rating.to_i
    puts "#{title} has been updated with a rating of 
#{rating}."
  end

when "display"
  movies.each do |movies, rating|
    puts "#{movies}: #{rating}"
  end

when "delete"
  puts "Please enter movie title:"
  title = gets.chomp.to_sym
  if movies[title.to_sym].nil?
    puts "Title doesn't exist"
  else
  movies.delete(title)
  puts "#{title} has been deleted!"
end

else
  puts "Error!"
end

1 个答案:

答案 0 :(得分:0)

您可以使用小写符号

movies = { starwars: 4.8, divergent: 4.7}

然后将用户输入转换为所有小写字母

  title = gets.chomp.downcase.to_sym

这会将标题(STARwarsStARwarS等)的大写形式的任何变化都转换为starwars