class Dry::Monads::Try::Value

Represents a result of a successful execution.

@api public

Attributes

catchable[R]

@return [Array<Exception>] List of exceptions to rescue

Public Class Methods

new(exceptions, value) click to toggle source

@param exceptions [Array<Exception>] list of exceptions to be rescued @param value [Object] the value to be stored in the monad

Calls superclass method
# File lib/dry/monads/try.rb, line 106
def initialize(exceptions, value)
  super()

  @catchable = exceptions
  @value = value
end

Public Instance Methods

bind(...) click to toggle source

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]

Calls superclass method Dry::Monads::RightBiased::Right#bind
# File lib/dry/monads/try.rb, line 132
def bind(...)
  super
rescue *catchable => e
  Error.new(e)
end
Also aliased as: bind_call
bind_call(...)
Alias for: bind
fmap(...) click to toggle source

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
inspect()
Alias for: to_s
recover(*_errors) click to toggle source

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
to_maybe() click to toggle source

@return [Maybe]

# File lib/dry/monads/maybe.rb, line 418
def to_maybe
  Dry::Monads::Maybe(@value)
end
to_result() click to toggle source

@return [Result::Success]

# File lib/dry/monads/result.rb, line 445
def to_result
  Dry::Monads::Result::Success.new(@value)
end
to_s() click to toggle source

@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
Also aliased as: inspect