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)


306
307
308
309
310
311
# File 'lib/dry/monads/try.rb', line 306

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)


289
290
291
292
293
294
# File 'lib/dry/monads/try.rb', line 289

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

  Try::Value.new(exceptions, v)
end