【问题标题】:Multiple Conditions While Loop Using .include? Ruby使用 .include 循环时的多个条件?红宝石
【发布时间】:2026-01-09 02:45:02
【问题描述】:

这里有很多关于多个 while 循环的好信息,但没有一个适用于 .include?​​p>

我正在尝试使以下 while 循环正常工作,但无济于事。如有任何帮助,我将不胜感激。

while browser.text.include? 'No results found for' || browser.text.include? '- did not match any documents.'

【问题讨论】:

  • 定义是什么让它“工作”。

标签: ruby watir


【解决方案1】:

您是否只需要添加括号?

例如:

[22] pry(main)> a=[1]
=> [1]
[23] pry(main)> a.include? 2
=> false
[24] pry(main)> a.include?2 || a.include?3
SyntaxError: unexpected tINTEGER, expecting end-of-input
[24] pry(main)> (a.include?2) || (a.include?3)
=> false
[25] pry(main)> 

【讨论】:

  • 我认为您的意思是需要“括号”而不是“父母”?
【解决方案2】:

我真的很喜欢将数据与逻辑分开,即使我们只有两个值。

no_result_strings = ['No results found for', '- did not match any documents.']

while no_result_strings.any?{|no_result| browser.text.include?(no_result)} do
    puts 'found no result, will continue.'
end

#Or, if browser.text is a string and you want it shorter:

while no_result_strings.any?{|x|browser.text[x]} do
    puts 'found no result, will continue.'
end

【讨论】:

    最近更新 更多