Need help with loops

Hello! Please help me to understand how can i get back from line 45 to
line 6 or how to repeat case when “else” comes.

movies = {
matrix: 5,
one: 4,
blade: 5
}
----------6------------
puts “what do you want?”
choice = gets.chomp
case choice
when “add”
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
puts “Such movie already exists”
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:

Hello! Please help me to understand how can i get back from line 45 to
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
    defined for the requested task.
  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,
blade: 5
}
----------6------------
puts “what do you want?”
choice = gets.chomp
case choice
when “add”
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
puts “Such movie already exists”
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,
after reading your answer. Then i found some other ways to solve this
problem. Ruby’s awesome :slight_smile:

Michael U. wrote in post #1161365:

Richard M. wrote in post #1161350:

Hello! Please help me to understand how can i get back from line 45 to
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
    defined for the requested task.
  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,
after reading your answer. Then i found some other ways to solve this
problem. Ruby’s awesome :slight_smile:

Ouch. Maybe it is, but really, can all the Ruby-enthusiasts once agree
to ban the word “awesome” from their vocabulary for a while…, let’s say
two years or so? Such a useful and completely normal word should not be
used-up the way it is in this community or “best ever” on YouTube.