Подтвердить что ты не робот

Ruby: Продолжить цикл после обнаружения исключения

В принципе, я хочу сделать что-то вроде этого (в Python или аналогичных императивных языках):

for i in xrange(1, 5):
    try:
        do_something_that_might_raise_exceptions(i)
    except:
        continue    # continue the loop at i = i + 1

Как это сделать в Ruby? Я знаю, что есть ключевые слова redo и retry, но они, похоже, повторно выполняют блок try, вместо того, чтобы продолжать цикл:

for i in 1..5
    begin
        do_something_that_might_raise_exceptions(i)
    rescue
        retry    # do_something_* again, with same i
    end
end
4b9b3361

Ответ 1

В Ruby continue написано next.

Ответ 2

for i in 1..5
    begin
        do_something_that_might_raise_exceptions(i)
    rescue
        next    # do_something_* again, with the next i
    end
end

Ответ 3

для печати исключения:

rescue
        puts $!, [email protected]
        next    # do_something_* again, with the next i
end