Skip to content

Instantly share code, notes, and snippets.

@liyanage
Created September 2, 2013 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liyanage/6409883 to your computer and use it in GitHub Desktop.
Save liyanage/6409883 to your computer and use it in GitHub Desktop.
Ruby cheat sheet

Retry helper

def with_retry(retry_limit=3)
  exception = nil
  retry_limit.times do |i|
    begin
      return yield()
    rescue Exception => ex
      print "Attempt #{i + 1} of #{retry_limit} failed: #{ex}\n"
      exception = ex
    end
  end
  raise exception
end

def some_function()
  return 'baz'
end

counter = 0
result = with_retry do
  counter += 1
  print "body #{counter}\n"
  if counter < 4
    raise Exception.new('bad')
  end
  some_function()
end

print "result: #{result}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment