如何在终端中保存我的.rb程序所做的更改?

时间:2015-04-10 22:22:58

标签: ruby crud

我真的很陌生,但是我写了一个小程序,将一部电影CRUD列入一个唯一的问题就是它没有保存所做的更改。我怎么能做到这一点?

error = "movie not found"
movies = {
    Mazerunner: 1
}
error = "movie not found"
puts "welcome to CrudMovies"
puts "enter a command"
puts "type add to add a movie to the list"
choice = gets.chomp.downcase

case choice
when "add"
    puts "what movie would you like to add"
        title = gets.chomp
    if movies[title.to_sym].nil?
        puts "what would you like the rating of #{rating} (1-4)to be?"
        rating = gets.chomp
        movies[title.to_sym] = rating.to_i
        puts "#{title} was added with a rating of #{rating}"
    else
        puts "that movie already exists"
end
when "update"
    puts "what movie would you like to update? (case sensitive)"
        title = gets.chomp
    if movies[title.to_sym].nil?
    puts "#{error}"
else
    puts "what is the movie rating would you like to update?"
    movies[title.to symb] = rating.to_i
    puts "#{title}'s rating has been updated to #{rating}"
end
when "display"
    movies.each do |x, y|
    puts "#{x} Rating:#{y}"
end
when "destroy"
    puts "what movie would you like to erase?"
        title = gets.chomp
    if movies[title.to_sym].nil?
        puts "#{error}"
    else
        movies.delete(title.to_sym)
    puts "the movie no longer exists"
end
    else
        puts "command not recognized"
end

1 个答案:

答案 0 :(得分:0)

我将假设" Save"你的意思是存储在movies哈希中。答案就是它实际上是存储的。执行操作后,只有您的脚本退出,因此您永远不会看到更新的movies

要查看所需的结果,您希望将其中的大部分包装在无限循环中,以防止脚本自然退出。

以下面的例子为例:

store = []
while true
  puts "Enter something:"
  choice = gets.chomp
  store.push choice
  puts "Your choices so far are: #{store.inspect}"
end