class Try

Attributes

result[RW]
task[RW]

Public Class Methods

new(&task) click to toggle source
# File lib/Olib/try/try.rb, line 10
def initialize(&task)
  @task = task
  run!
end
of(&task) click to toggle source
# File lib/Olib/try/try.rb, line 4
def self.of(&task)
  Proc.new do 
    Try.new task
  end
end

Public Instance Methods

failed?() click to toggle source
# File lib/Olib/try/try.rb, line 29
def failed?
  @result.class.ancestors.include? Exception
end
match?(exp) click to toggle source
# File lib/Olib/try/try.rb, line 45
def match?(exp)
  if failed?
    @result.message.match(exp)
  elsif @result.respond_to?(:match)
    @result.match(exp)
  else
    raise Exception.new "cannot match class #{@result.class}"
  end
end
recover() { |result| ... } click to toggle source
# File lib/Olib/try/try.rb, line 37
def recover
  Try.new { yield @result } if failed?
end
success?() click to toggle source
# File lib/Olib/try/try.rb, line 33
def success?
  !failed?
end
then() { |result| ... } click to toggle source
# File lib/Olib/try/try.rb, line 41
def then
  Try.new { yield @result } if success?
end

Private Instance Methods

run!() click to toggle source
# File lib/Olib/try/try.rb, line 15
        def run!
  begin
    result = @task.call
    # handle recursives
    if result.is_a?(Try)
      @result = result.result
    else
      @result = result
    end
  rescue Exception => e
    @result = e
  end
end