# Need help with loops

line 6 or how to repeat case when “else” comes.

movies = {
matrix: 5,
one: 4,
}
----------6------------
puts “what do you want?”
choice = gets.chomp
case choice
puts “Title?”
title = gets.chomp
if movies[title.to_sym].nil?
puts “Rating?”
rating = gets.chomp
movies[title.to_sym]=rating.to_i
puts “#{title} = #{rating}”
else
end
when “update”
puts “Movie title?”
title = gets.chomp
if movies[title.to_sym].nil?
puts “There is no such movie”
else
puts “enter new rating”
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts “#{title} = #{movies[title.to_sym]}”
end
when “display”
movies.each {|movie,rating| puts “#{movie}: #{rating}”}
when “delete”
puts “What movie do you want to delete?”
title = gets.chomp
if movies[title.to_sym].nil?
puts “There is no such movie”
else
movies.delete(title.to_sym)
puts “#{movies[title.to_sym]}”
end
else
puts “I don’t understand you. Try again”
---------45---------
end

Richard M. wrote in post #1161350:

line 6 or how to repeat case when “else” comes.

As you can imagine, there are as many ways to solve this problem, as
there are readers in the forum. I will not provide you with a solution
but just suggest an approach for you to elaborate.

1. Initialize a variable like “action” with nil (action = nil)
2. Implement each of the „when“ branches as a Proc-object to call.
3. Write a loop: until(action) do
4. Use the initial user-input to determine if there is a suitable Proc
5. If there is, first allocate this object to “action”, then …
6. … call the Proc (action.call), possibly with arguments.
7. If action is still nil, write your “try again”
8. end
------------------ original code sample --------------

movies = {
matrix: 5,
one: 4,
}
----------6------------
puts “what do you want?”
choice = gets.chomp
case choice
puts “Title?”
title = gets.chomp
if movies[title.to_sym].nil?
puts “Rating?”
rating = gets.chomp
movies[title.to_sym]=rating.to_i
puts “#{title} = #{rating}”
else
end
when “update”
puts “Movie title?”
title = gets.chomp
if movies[title.to_sym].nil?
puts “There is no such movie”
else
puts “enter new rating”
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts “#{title} = #{movies[title.to_sym]}”
end
when “display”
movies.each {|movie,rating| puts “#{movie}: #{rating}”}
when “delete”
puts “What movie do you want to delete?”
title = gets.chomp
if movies[title.to_sym].nil?
puts “There is no such movie”
else
movies.delete(title.to_sym)
puts “#{movies[title.to_sym]}”
end
else
puts “I don’t understand you. Try again”
---------45---------
end

Thanks for help, Michael. I realize how i need to think in this case,
problem. Ruby’s awesome

Michael U. wrote in post #1161365:

Richard M. wrote in post #1161350:

line 6 or how to repeat case when “else” comes.

As you can imagine, there are as many ways to solve this problem, as
there are readers in the forum. I will not provide you with a solution
but just suggest an approach for you to elaborate.

1. Initialize a variable like “action” with nil (action = nil)
2. Implement each of the „when“ branches as a Proc-object to call.
3. Write a loop: until(action) do
4. Use the initial user-input to determine if there is a suitable Proc
5. If there is, first allocate this object to “action”, then …
6. … call the Proc (action.call), possibly with arguments.
7. If action is still nil, write your “try again”
8. end

You’re welcome

Richard M. wrote in post #1161425:

Thanks for help, Michael. I realize how i need to think in this case,