Skip to content

Instantly share code, notes, and snippets.

@cmdrkeene
Created August 4, 2009 15:18
Show Gist options
  • Save cmdrkeene/161304 to your computer and use it in GitHub Desktop.
Save cmdrkeene/161304 to your computer and use it in GitHub Desktop.
Forgive us, for we have sinned
# Execute a block +number+ times. Block should be a predicate (return true/false)
#
# Use procs/lambdas to define callbacks:
# :success => ...
# :retry => ...
# :failure => ...
def do_over(number, options = {}, &block)
options = {
:success => lambda { puts "SUCCESS" },
:retry => lambda { puts "RETRY" },
:failure => lambda { puts "FAILURE"; raise "Giving up!" }
}.merge(options)
number.times do |n|
if yield
options[:success].call(n)
break
else
if n == (number - 1)
options[:failure].call(n)
else
options[:retry].call(n)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment