Class: Dry::Types::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/types/result.rb,
lib/dry/types/extensions/monads.rb

Overview

Monad extension for Result

Direct Known Subclasses

Failure, Success

Defined Under Namespace

Classes: Failure, Success

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Result

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Result.

Parameters:

  • input (Object)


17
18
19
# File 'lib/dry/types/result.rb', line 17

def initialize(input)
  @input = input
end

Instance Attribute Details

#inputObject (readonly)

Returns:

  • (Object)


12
13
14
# File 'lib/dry/types/result.rb', line 12

def input
  @input
end

Instance Method Details

#to_monadDry::Monads::Success, Dry::Monads::Failure

Turn result into a monad

This makes result objects work with dry-monads (or anything with a compatible interface)

Returns:

  • (Dry::Monads::Success, Dry::Monads::Failure)


25
26
27
28
29
30
31
# File 'lib/dry/types/extensions/monads.rb', line 25

def to_monad
  if success?
    Success(input)
  else
    Failure([error, input])
  end
end