Class: Fixnum

Inherits:
Object show all
Defined in:
lib/extensions/fixnum.rb

Instance Method Summary collapse

Instance Method Details

#tries(*options) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/extensions/fixnum.rb', line 2

def tries(*options)
  rescueable_types = []
  fail_proc = nil

  options.each do |option|
    rescueable_types << option  if option.is_a? Class and option < Exception
    fail_proc = option  if option.is_a? Proc
  end

  rescueable_types << Exception  if rescueable_types.empty?

  self.times do |i|
    begin
      return yield
    rescue *rescueable_types
      raise  if (self-1) == i
      fail_proc.call  if fail_proc
    end
  end
end