<commit_before># Tries

[![Build Status](https://secure.travis-ci.org/krautcomputing/tries.png)](http://travis-ci.org/krautcomputing/tries)
[![Dependency Status](https://gemnasium.com/krautcomputing/tries.png)](https://gemnasium.com/krautcomputing/tries)
[![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/krautcomputing/tries)

Solidify your code and retry on petty exceptions

## Requirements

Requires Ruby 1.9.2 or higher

## Is it production ready?

Yes! I have been using this code in numerous applications for several years.

## Installation

Add this line to your application's Gemfile:

    gem 'tries'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install tries

## Usage

```ruby
3.tries on: Timeout::Error do
  Mechanize.new.get 'https://www.google.com/'
end
```

## Detailed usage

```ruby
# Helper code to explain how it works

FooError = Class.new(StandardError)
BarError = Class.new(StandardError)

@counter = 0

def method_that_raises_exception
  @counter += 1
  puts "Counter is #{@counter}"

  case @counter
  when 1 then raise FooError
  when 2 then raise FooError
  when 3 then raise BarError
  when 4 then raise StandardError
  end

  puts 'You made it through!'
end
```

```ruby
# Rescue all errors

4.tries do
  method_that_raises_exception
end

> Counter is 1
> Counter is 2
> Counter is 3
> Counter is 4
> Counter is 5
> You made it through!
```

```ruby
# Rescue only certain errors

3.tries on: FooError do
  method_that_raises_exception
end

> Counter is 1
> Counter is 2
> Counter is 3
> BarError: BarError

3.tries on: [FooError, BarError] do
  method_that_raises_exception
end

> Counter is 1
> Counter is 2
> Counter is 3
> Counter is 4
> StandardError: StandardError
```

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request<commit_msg>Update README, add examples for "delay" parameter<commit_after># Tries

[![Build Status](https://secure.travis-ci.org/krautcomputing/tries.png)](http://travis-ci.org/krautcomputing/tries)
[![Dependency Status](https://gemnasium.com/krautcomputing/tries.png)](https://gemnasium.com/krautcomputing/tries)
[![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/krautcomputing/tries)

Solidify your code and retry on petty exceptions

## Requirements

Requires Ruby 1.9.2 or higher

## Is it production ready?

Yes! I have been using this code in numerous applications for several years.

## Installation

Add this line to your application's Gemfile:

    gem 'tries'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install tries

## Usage

```ruby
3.tries on: Timeout::Error do
  Mechanize.new.get 'https://www.google.com/'
end
```

## Detailed usage

```ruby
# Helper code to explain how it works

FooError = Class.new(StandardError)
BarError = Class.new(StandardError)

@error_counter = 0

def method_that_raises_exception
  @error_counter += 1
  puts "Counter is #{@error_counter}"

  case @error_counter
  when 1 then raise FooError
  when 2 then raise FooError
  when 3 then raise BarError
  when 4 then raise StandardError
  end

  puts 'You made it through!'
end
```

```ruby
# Rescue all errors
4.tries do
  method_that_raises_exception
end

=> Counter is 1
=> Counter is 2
=> Counter is 3
=> Counter is 4
=> Counter is 5
=> You made it through!
```

```ruby
# Rescue a specific error
3.tries on: FooError do
  method_that_raises_exception
end

=> Counter is 1
=> Counter is 2
=> Counter is 3
=> BarError: BarError
```

```ruby
# Rescue multiple errors
3.tries on: [FooError, BarError] do
  method_that_raises_exception
end

=> Counter is 1
=> Counter is 2
=> Counter is 3
=> Counter is 4
=> StandardError: StandardError
```

```ruby
# Delay execution after error
# "delay" parameter is in seconds, fractions are possible
4.tries delay: 1.5 do
  method_that_raises_exception
end

=> Counter is 1
waits 1.5 seconds...
=> Counter is 2
waits 1.5 seconds...
=> Counter is 3
waits 1.5 seconds...
=> Counter is 4
waits 1.5 seconds...
=> Counter is 5
=> You made it through!
```

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
{'fraction_non_alphanumeric': 0.11021865238732709, 'fraction_numerical': 0.016956715751896476, 'mean_word_length': 4.071266968325792, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, 'https://': 12, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1105142', 'n_tokens_mistral': 1621, 'n_tokens_neox': 1468, 'n_words': 519}