Skip to content

Ruby Keyword: retry

☈king edited this page Nov 19, 2012 · 3 revisions

Re-run the block that raised an Exception.

For example:

tries = 3
begin
  puts "Try ##{tries}."
  1/0
rescue => e
  tries -= 1
  retry unless tries.zero?
end

This will output:

Try #3.
Try #2.
Try #1.

See also: begin, rescue, else, ensure

Clone this wiki locally