Skip to content

Instantly share code, notes, and snippets.

@kouba-c
Created November 14, 2013 12:31
Show Gist options
  • Save kouba-c/7466019 to your computer and use it in GitHub Desktop.
Save kouba-c/7466019 to your computer and use it in GitHub Desktop.
class MyError < StandardError; end
def repeat_block(max_repeat, &block)
challange = Proc.new do |cnt|
begin
block.call
puts "success."
rescue MyError => e
if cnt > 0
challange.call(cnt-1)
else
puts "failure."
raise e
end
end
end
challange.call(max_repeat)
end
repeat_block(3) do
puts "start."
raise MyError unless rand(3) == 0
puts "end."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment