Looping back to CRUD interface

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?

until exit do
… do stuff…
end

}
puts “Now rate it”
else
if movies[title.to_sym].nil?

#how to make it loop back to beginning?

On Wed, May 8, 2013 at 11:09 PM, Cliff R. [email protected]
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.

AHA! Thank you so much tamouse. It worked just like I wanted :slight_smile:

I know nothing about gems, but I’ll definitely look into it. Thanks
again!