2
在Python,else块将被执行,除非循环破产了:Python在循环中支持`else`。 Ruby中是否有类似的功能?
for person in people:
if is_a_coder(person): break
# ... more code ...
else: # nobreak
print('There is no coder.')
我怎么能做到这一点的红宝石?
在Python,else块将被执行,除非循环破产了:Python在循环中支持`else`。 Ruby中是否有类似的功能?
for person in people:
if is_a_coder(person): break
# ... more code ...
else: # nobreak
print('There is no coder.')
我怎么能做到这一点的红宝石?
只需使用and
:
people.each do |person|
break if is_a_coder(person)
# more code
end and puts 'There is no coder.'
有码味约这一点。 – 2014-12-09 16:37:05