尝试在 case 语句中引用较早的 'case'

Trying to reference earlier 'case' in a case statement

当有人试图更新当前未存储在我的哈希中的值时,我想立即返回参考 when 'add' 而无需重新启动整个 case 语句,因为我已经知道他们想要添加并且不想再次提示他们。

有没有办法在不重新启动整个 case 语句的情况下返回我代码的 case choice -> when "add" 部分?

我知道我可以使用嵌套的 case 语句,但如果不需要,我宁愿不使用 copy/paste 相同的代码。

hash = {}
puts "Would you like to add or update this hash?"
choice = gets.chomp
case choice
when "add"
  puts "What key you like to add?"
  key = gets.chomp
  puts "With what value?"
  value = gets.chomp
  hash[key] = value
when "update"
  puts "Which key would you like to update?"
  key = gets.chomp
  if hash[key].nil?
  puts "Key not present, would you like to add it?"
    #here I would like the code that references back to "when 'add'" if the user types 'yes'    

抱歉代码突然结束。我不想在解决方案中加入任何不必要的东西。

创建一个 method/function 将功能包装在该案例中。然后你可以从两个地方调用那个函数

hash = {}
def add_key
  puts "What key you like to add?"
  key = gets.chomp
  puts "With what value?"
  value = gets.chomp
  hash[key] = value
end 
puts "Would you like to add or update this hash?"
choice = gets.chomp
case choice
when "add"
  add_key
when "update"
  puts "Which key would you like to update?"
  key = gets.chomp
  if hash[key].nil?
    puts "Key not present, would you like to add it?"
    add_key