Module: Dry::Monads::Try::Mixin

Includes:
Constructors
Defined in:
lib/dry/monads/try.rb

Overview

A module that can be included for easier access to Try monads.

Examples:

class Foo
  include Dry::Monads::Try::Mixin

  attr_reader :average

  def initialize(total, count)
    @average = Try(ZeroDivisionError) { total / count }.value
  end
end

Foo.new(10, 2).average # => 5
Foo.new(10, 0).average # => nil

Defined Under Namespace

Modules: Constructors

Constant Summary collapse

Try =

See Also:

Try

Instance Method Summary collapse

Methods included from Constructors

#Try

Instance Method Details

#Error(value) ⇒ Try::Error #Error(&block) ⇒ Try::Error

Error constructor

Overloads:

Raises:

  • (ArgumentError)

268
269
270
271
272
# File 'lib/dry/monads/try.rb', line 268

def Error(error = Undefined, &block)
  v = Undefined.default(error, block)
  raise ArgumentError, 'No value given' if v.nil?
  Try::Error.new(v)
end

#Value(value) ⇒ Try::Value #Value(&block) ⇒ Try::Value

Value constructor

Overloads:

Raises:

  • (ArgumentError)

252
253
254
255
256
# File 'lib/dry/monads/try.rb', line 252

def Value(value = Undefined, exceptions = DEFAULT_EXCEPTIONS, &block)
  v = Undefined.default(value, block)
  raise ArgumentError, 'No value given' if !value.nil? && v.nil?
  Value.new(exceptions, v)
end