Class: Dry::Monads::Try::Value
- Inherits:
-
Dry::Monads::Try
- Object
- Dry::Monads::Try
- Dry::Monads::Try::Value
- 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
Instance Attribute Summary collapse
- #catchable ⇒ Object readonly
Attributes inherited from Dry::Monads::Try
Instance Method Summary collapse
-
#bind(*args) ⇒ Object, Try::Error
Calls the passed in Proc object with value stored in self and returns the result.
-
#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.
-
#initialize(exceptions, value) ⇒ Value
constructor
A new instance of Value.
- #to_maybe ⇒ Maybe
- #to_result ⇒ Result::Success
- #to_s ⇒ String (also: #inspect)
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.
109 110 111 112 |
# File 'lib/dry/monads/try.rb', line 109 def initialize(exceptions, value) @catchable = exceptions @value = value end |
Instance Attribute Details
#catchable ⇒ Object (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.
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.
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_maybe ⇒ Maybe
267 268 269 |
# File 'lib/dry/monads/maybe.rb', line 267 def to_maybe Dry::Monads::Maybe(@value) end |
#to_result ⇒ Result::Success
336 337 338 |
# File 'lib/dry/monads/result.rb', line 336 def to_result Dry::Monads::Result::Success.new(@value) end |
#to_s ⇒ String Also known as: inspect
158 159 160 |
# File 'lib/dry/monads/try.rb', line 158 def to_s "Try::Value(#{ @value.inspect })" end |