module Dry::Monads::Try::Mixin
A module that can be included for easier access to Try
monads.
@example
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
Constants
- Try
Represents a value which can be either success or a failure (an exception). Use it to wrap code that can raise exceptions.
@api public
Public Instance Methods
Error(error = Undefined, &block)
click to toggle source
Error
constructor
@overload Error(value)
@param error [Exception] @return [Try::Error]
@overload Error(&block)
@param block [Proc] a block to be wrapped with Error @return [Try::Error]
# 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 = Undefined, exceptions = DEFAULT_EXCEPTIONS, &block)
click to toggle source
Value
constructor
@overload Value(value)
@param value [Object] @return [Try::Value]
@overload Value(&block)
@param block [Proc] a block to be wrapped with Value @return [Try::Value]
# 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