Created
August 4, 2009 15:18
-
-
Save cmdrkeene/161304 to your computer and use it in GitHub Desktop.
Forgive us, for we have sinned
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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