在字符串中检测到特定术语/单词时停止while循环

我想知道如何在ruby中创建一个代码,只要获取字符串包含单词“yes”,就可以停止while循环而不管写什么。

这是我到目前为止:

while start!="yes" #make it so it stops the loop even if ex:"Yes, please!!!" is inputed.
  #does stuff
  start=gets.chomp
end

非常感谢你。

采纳答案:

我认为这会起作用,顺便说一下, untilwhile但是有负面条件。

input = gets.chomp.downcase

until input.include? 'yes'
  # does stuff
  input = gets.chomp.downcase
end

参考更多解答: Stop a while loop when a specific term/word is detected in the string ,转载请保留出处在字符串中检测到特定术语/单词时停止while循环及作者信息

Statement: We respect knowledge and authors. Since the content comes from the Internet and is intended for scientific research, any reprinters should retain the author's signature and origin. If you are the author of the content and feel in dispute, please contact email: 1076545519@qq.com. We will find out the situation and deal with it in time. We sincerely thank the author for his hard work.


更多:loops