From: tamouse mailing lists Date: 2013-05-09T13:17:31+09:00 Subject: Re: Looping back to CRUD interface > On May 8, 2013 5:41 PM, "Michael Bowen" wrote: >> >> How can I return the program back to the CRUD menu if the user wants to >> take another action? I thought about using returns, but I'm not quite >> sure how that would work. >> >> #movie hash >> movies = { superbad: 4, >> miami_vice: 0, >> juno: 0 >> } >> #Get CRUD input >> puts "Do you want to add, update, display, delete or exit?" >> choice = gets.chomp.downcase >> >> #case since there are many conditions/options >> case choice >> when 'add' >> puts "Give me a movie title" >> title = gets.chomp >> if movies[title.to_sym].nil? >> puts "Now rate it" >> rating = gets.chomp >> movies[title.to_sym] = rating.to_i >> else >> puts "That movie already exists" >> end >> when "update" >> puts "Give me the title." >> title = gets.chomp >> if movies[title.to_sym].nil? >> puts "That movie does not exist" >> else >> puts "What is the new rating?" >> rating = gets.chomp >> movies[title.to_sym] = rating.to_i >> puts "#{rating} is the new rating!" >> end >> when "display" >> movies.each { |x , y| puts "#{x}: #{y}"} >> when "delete" >> puts "What is the title you want to remove?!" >> title = gets.chomp >> if movies[title.to_sym].nil? >> puts "Error, movie is not listed." >> else >> movies.delete(title.to_sym) >> puts "#{title} has been removed" >> end >> when 'exit' >> return 0 >> else >> puts "Error!" >> end >> >> >> #how to make it loop back to beginning? On Wed, May 8, 2013 at 11:09 PM, Cliff Rosson wrote: > until exit do > ... do stuff... > end > err..um..err.. `exit` is a method on Kernel that causes the script to exit immediately. Probably you had something else in mind? loop do # code-n-stuff when 'exit' break # causes loop to stop else puts 'Error!' end end To OP: investigate the highline gem for dealing with nice I/O in command line scripts.