module Fear::TryApi

Public Instance Methods

failure(exception) click to toggle source

@param exception [StandardError] @return [Fear::Failure]

# File lib/fear/try_api.rb, line 22
def failure(exception)
  Fear::Failure.new(exception)
end
success(value) click to toggle source

@param value [any] @return [Fear::Success]

# File lib/fear/try_api.rb, line 29
def success(value)
  Fear::Success.new(value)
end
try() { || ... } click to toggle source

Constructs a Try using the block. This method ensures any non-fatal exception is caught and a Failure object is returned. @return [Fear::Try] @example

Fear.try { 4/0 } #=> #<Fear::Failure exception=#<ZeroDivisionError: divided by 0>>
Fear.try { 4/2 } #=> #<Fear::Success value=2>
# File lib/fear/try_api.rb, line 13
def try
  success(yield)
rescue StandardError => error
  failure(error)
end