Class: Danom::Default
Overview
A default “monad”. Not technically a monad as it take two values.
Instance Method Summary collapse
-
#and_then(&block) ⇒ Object
Allows chaining on nil values with a fallback.
-
#initialize(fallback, value) ⇒ Default
constructor
A new instance of Default.
- #monad_value ⇒ Object
Methods inherited from Monad
Constructor Details
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Danom::Monad
Instance Method Details
#and_then(&block) ⇒ Object
Note:
The fallback does not get the transformations
Allows chaining on nil values with a fallback
Default can be combined with a maybe. Extracting the value is recursive and will never return another monad.
41 42 43 44 45 46 47 |
# File 'lib/danom/default.rb', line 41 def and_then &block if @value == nil Default.new @fallback, nil else Default.new @fallback, block.call(@value) end end |
#monad_value ⇒ Object
15 16 17 |
# File 'lib/danom/default.rb', line 15 def monad_value super || @fallback end |