From: Cliff Rosson Date: 2013-05-09T13:09:22+09:00 Subject: Re: Looping back to CRUD interface --bcaec520f13911d9cf04dc41373b Content-Type: text/plain; charset=ISO-8859-1 until exit do ... do stuff... end 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? > > -- > Posted via http://www.ruby-forum.com/. > > --bcaec520f13911d9cf04dc41373b Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

until exit do
... do stuff...
end

On May 8, 2013 5:41 PM, "Michael Bowen"= ; <lists@ruby-forum.com> = 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 =3D { superbad: 4,
miami_vice: 0,
juno: 0
}
#Get CRUD input
puts "Do you want to add, update, display, delete or exit?"
choice =3D gets.chomp.downcase

#case since there are many conditions/options
case choice
when 'add'
=A0 =A0 puts "Give me a movie title"
=A0 =A0 title =3D gets.chomp
=A0 =A0 if movies[title.to_sym].nil?
=A0 =A0 =A0 =A0 puts "Now rate it"
=A0 =A0 =A0 =A0 rating =3D gets.chomp
=A0 =A0 =A0 =A0 movies[title.to_sym] =3D rating.to_i
=A0 =A0 else
=A0 =A0 =A0 =A0 puts "That movie already exists"
=A0 =A0 end
when "update"
=A0 =A0 puts "Give me the title."
=A0 =A0 title =3D gets.chomp
=A0 =A0 if movies[title.to_sym].nil?
=A0 =A0 =A0 =A0 puts "That movie does not exist"
=A0 =A0 else
=A0 =A0 =A0 =A0 puts "What is the new rating?"
=A0 =A0 =A0 =A0 rating =3D gets.chomp
=A0 =A0 =A0 =A0 movies[title.to_sym] =3D rating.to_i
=A0 =A0 =A0 =A0 puts "#{rating} is the new rating!"
=A0 =A0 end
when "display"
=A0 =A0 movies.each { |x , y| puts "#{x}: #{y}"}
when "delete"
=A0 =A0 puts "What is the title you want to remove?!"
=A0 =A0 title =3D gets.chomp
=A0 =A0 if movies[title.to_sym].nil?
=A0 =A0 =A0 =A0 puts "Error, movie is not listed."
=A0 =A0 else
=A0 =A0 =A0 =A0 movies.delete(title.to_sym)
=A0 =A0 =A0 =A0 puts "#{title} has been removed"
=A0 =A0 end
when 'exit'
=A0 return 0
else
=A0 =A0 puts "Error!"
end


#how to make it loop back to beginning?

--
Posted via http://= www.ruby-forum.com/.

--bcaec520f13911d9cf04dc41373b--