Class: Monads::Maybe

Inherits:
Object
  • Object
show all
Includes:
Monad
Defined in:
lib/ruby-monads/maybe.rb

Direct Known Subclasses

Just, Nothing

Class Method Summary collapse

Instance Method Summary collapse

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



6
7
8
# File 'lib/ruby-monads/maybe.rb', line 6

def self.unit(value)
  value.nil? || value.is_a?(Nothing) ? Nothing.new : Just.new(value)
end

Instance Method Details

#bind(&block) ⇒ Object

bind

(a -> M b) -> M a -> M b



11
12
13
# File 'lib/ruby-monads/maybe.rb', line 11

def bind(&block)
  ensure_monadic_result(&block).call
end

#unwrap(default_value) ⇒ Object

unwrap

a -> M a -> a



16
17
18
# File 'lib/ruby-monads/maybe.rb', line 16

def unwrap(default_value)
  @value || default_value
end