class Dry::Monads::Try::Value
Represents a result of a successful execution.
@api public
Attributes
@return [Array<Exception>] List
of exceptions to rescue
Public Class Methods
@param exceptions [Array<Exception>] list of exceptions to be rescued @param value [Object] the value to be stored in the monad
# File lib/dry/monads/try.rb, line 106 def initialize(exceptions, value) super() @catchable = exceptions @value = value end
Public Instance Methods
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.
@example
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)
@param 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
@return [Object, Try::Error
]
Dry::Monads::RightBiased::Right#bind
# File lib/dry/monads/try.rb, line 132 def bind(...) super rescue *catchable => e Error.new(e) end
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.
@example
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
@param args [Array<Object>] extra arguments for the block, arguments are being processes
just as in #bind
@return [Try::Value, Try::Error
]
# File lib/dry/monads/try.rb, line 150 def fmap(...) Value.new(catchable, bind_call(...)) rescue *catchable => e Error.new(e) end
Ignores values and returns self, see {Try::Error#recover}
@param errors [Class] List
of Exception subclasses
@return [Try::Value]
# File lib/dry/monads/try.rb, line 171 def recover(*_errors) self end
@return [Maybe]
# File lib/dry/monads/maybe.rb, line 418 def to_maybe Dry::Monads::Maybe(@value) end
@return [Result::Success]
# File lib/dry/monads/result.rb, line 445 def to_result Dry::Monads::Result::Success.new(@value) end
@return [String]
# File lib/dry/monads/try.rb, line 157 def to_s if Unit.equal?(@value) "Try::Value()" else "Try::Value(#{@value.inspect})" end end