Class: Monads::Result
Constant Summary collapse
- FAILURE_TRIGGER =
StandardError
Class Method Summary collapse
-
.unit(value) ⇒ Object
- unit
-
a -> M a.
Instance Method Summary collapse
-
#bind(&block) ⇒ Object
- bind
-
(a -> M b) -> M a -> M b.
-
#unwrap(default_value = @value) ⇒ Object
- unwrap
-
a -> M a -> a.
Methods included from Monad
#fmap, included, #initialize, #join, #method_missing
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Monads::Monad
Class Method Details
.unit(value) ⇒ Object
- unit
-
a -> M a
8 9 10 |
# File 'lib/monads/result.rb', line 8 def self.unit(value) value.is_a?(FAILURE_TRIGGER) ? Failure.new(value) : Success.new(value) end |
Instance Method Details
#bind(&block) ⇒ Object
- bind
-
(a -> M b) -> M a -> M b
13 14 15 16 17 18 19 20 21 |
# File 'lib/monads/result.rb', line 13 def bind(&block) return self if is_a?(Failure) begin ensure_monadic_result(&block).call rescue FAILURE_TRIGGER => error Failure.new(error) end end |
#unwrap(default_value = @value) ⇒ Object
- unwrap
-
a -> M a -> a
24 25 26 |
# File 'lib/monads/result.rb', line 24 def unwrap(default_value = @value) is_a?(Failure) ? default_value : @value end |