Class: Dry::Monads::Try::Value

Inherits:
Dry::Monads::Try show all
Includes:
RightBiased::Right
Defined in:
lib/dry/monads/try.rb,
lib/dry/monads/maybe.rb,
lib/dry/monads/either.rb,
lib/dry/monads/result.rb

Overview

Represents a result of a successful execution.

Constant Summary

Constants inherited from Dry::Monads::Try

DEFAULT_EXCEPTIONS

Instance Attribute Summary collapse

Attributes inherited from Dry::Monads::Try

#exception

Instance Method Summary collapse

Methods included from RightBiased::Right

#===, #apply, #discard, included, #or, #or_fmap, #tee, #value!, #value_or

Methods inherited from Dry::Monads::Try

[], #error?, pure, run, #to_monad, #value?

Constructor Details

#initialize(exceptions, value) ⇒ Value

Returns a new instance of Value.

Parameters:

  • exceptions (Array<Exception>)

    list of exceptions to be rescued

  • value (Object)

    the value to be stored in the monad



109
110
111
112
# File 'lib/dry/monads/try.rb', line 109

def initialize(exceptions, value)
  @catchable = exceptions
  @value = value
end

Instance Attribute Details

#catchableObject (readonly)



105
106
107
# File 'lib/dry/monads/try.rb', line 105

def catchable
  @catchable
end

Instance Method Details

#bind(*args) ⇒ Object, Try::Error

Calls the passed in Proc object with value stored in self and returns the result.

If proc is nil, it expects a block to be given and will yield to it.

Examples:

success = Dry::Monads::Try::Value.new(ZeroDivisionError, 10)
success.bind(->(n) { n / 2 }) # => 5
success.bind { |n| n / 0 } # => Try::Error(ZeroDivisionError: divided by 0)

Parameters:

  • args (Array<Object>)

    arguments that will be passed to a block if one was given, otherwise the first value assumed to be a Proc (callable) object and the rest of args will be passed to this object along with the internal value

Returns:



133
134
135
136
137
# File 'lib/dry/monads/try.rb', line 133

def bind(*args)
  super
rescue *catchable => e
  Error.new(e)
end

#fmap(*args, &block) ⇒ Try::Value, Try::Error

Does the same thing as #bind except it also wraps the value in an instance of a Try monad. This allows for easier chaining of calls.

Examples:

success = Dry::Monads::Try::Value.new(ZeroDivisionError, 10)
success.fmap(&:succ).fmap(&:succ).value # => 12
success.fmap(&:succ).fmap { |n| n / 0 }.fmap(&:succ).value # => nil

Parameters:

  • args (Array<Object>)

    extra arguments for the block, arguments are being processes just as in #bind

Returns:



151
152
153
154
155
# File 'lib/dry/monads/try.rb', line 151

def fmap(*args, &block)
  Value.new(catchable, bind_call(*args, &block))
rescue *catchable => e
  Error.new(e)
end

#to_maybeMaybe

Returns:



267
268
269
# File 'lib/dry/monads/maybe.rb', line 267

def to_maybe
  Dry::Monads::Maybe(@value)
end

#to_resultResult::Success

Returns:



336
337
338
# File 'lib/dry/monads/result.rb', line 336

def to_result
  Dry::Monads::Result::Success.new(@value)
end

#to_sString Also known as: inspect

Returns:

  • (String)


158
159
160
# File 'lib/dry/monads/try.rb', line 158

def to_s
  "Try::Value(#{ @value.inspect })"
end