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
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.
Initialize a variable like “action” with nil (action = nil)
Implement each of the „when“ branches as a Proc-object to call.
Write a loop: until(action) do
Use the initial user-input to determine if there is a suitable Proc
defined for the requested task.
If there is, first allocate this object to “action”, then …
… call the Proc (action.call), possibly with arguments.
If action is still nil, write your “try again”
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
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.
Initialize a variable like “action” with nil (action = nil)
Implement each of the „when“ branches as a Proc-object to call.
Write a loop: until(action) do
Use the initial user-input to determine if there is a suitable Proc
defined for the requested task.
If there is, first allocate this object to “action”, then …
… call the Proc (action.call), possibly with arguments.
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
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.