gpt4 book ai didi

Python 在循环中支持 `else`。 Ruby 中是否有类似的功能?

转载 作者:数据小太阳 更新时间:2023-10-29 08:22:36 24 4
gpt4 key购买 nike

在 Python 中,除非循环中断,否则将执行 else block :

for person in people:
if is_a_coder(person): break
# ... more code ...
else: # nobreak
print('There is no coder.')

我如何在 Ruby 中做到这一点?

最佳答案

只需使用:

people.each do |person|
break if is_a_coder(person)
# more code
end and puts 'There is no coder.'

关于Python 在循环中支持 `else`。 Ruby 中是否有类似的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27377964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com