Python在循环中支持`else`.Ruby中有类似的功能吗?

wea*_*ish 2 ruby python loops if-statement break

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

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

我怎么能在Ruby中做到这一点?

wea*_*ish 7

只需使用and:

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