Module: Fear::TryApi

Included in:
Fear
Defined in:
lib/fear/try_api.rb

Instance Method Summary collapse

Instance Method Details

#failure(exception) ⇒ Fear::Failure

Parameters:

  • exception (StandardError)

Returns:



24
25
26
# File 'lib/fear/try_api.rb', line 24

def failure(exception)
  Fear::Failure.new(exception)
end

#success(value) ⇒ Fear::Success

Parameters:

  • value (any)

Returns:



31
32
33
# File 'lib/fear/try_api.rb', line 31

def success(value)
  Fear::Success.new(value)
end

#tryFear::Try

Constructs a Try using the block. This method ensures any non-fatal exception is caught and a Failure object is returned.

Examples:

Fear.try { 4/0 } #=> #<Fear::Failure exception=#<ZeroDivisionError: divided by 0>>
Fear.try { 4/2 } #=> #<Fear::Success value=2>

Returns:



15
16
17
18
19
# File 'lib/fear/try_api.rb', line 15

def try
  success(yield)
rescue StandardError => error
  failure(error)
end