可能重复:
检查Ruby中数组中是否存在值
我有这个循环遍历字符串数组的方法,如果任何字符串包含字符串'dog',则返回true.它正在工作,但多个返回语句看起来很混乱.有没有更有说服力的方式这样做?
def has_dog?(acct)
[acct.title, acct.description, acct.tag].each do |text|
return true if text.include?("dog")
end
return false
end
Run Code Online (Sandbox Code Playgroud)
Ser*_*sev 51
def has_dog?(acct)
[acct.title, acct.description, acct.tag].any? { |text| text.include? "dog" }
end
Run Code Online (Sandbox Code Playgroud)
它将返回true/ false.
| 归档时间: |
|
| 查看次数: |
15676 次 |
| 最近记录: |