Module: Monads::Monad

Included in:
Maybe, Result
Defined in:
lib/ruby-monads/monad.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



26
27
28
29
30
# File 'lib/ruby-monads/monad.rb', line 26

def method_missing(method, *args, &block)
  fmap do |value|
    value.public_send(method, *args, &block)
  end
end

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
# File 'lib/ruby-monads/monad.rb', line 3

def self.included(base)
  base.class_eval do
    private_class_method :new
  end
end

Instance Method Details

#fmap(&block) ⇒ Object

fmap

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



14
15
16
17
18
# File 'lib/ruby-monads/monad.rb', line 14

def fmap(&block)
  bind do |value|
    self.class.unit(block.call(value))
  end
end

#initialize(value) ⇒ Object



9
10
11
# File 'lib/ruby-monads/monad.rb', line 9

def initialize(value)
  @value = value
end

#joinObject

join

M (M a) -> M a



21
22
23
24
# File 'lib/ruby-monads/monad.rb', line 21

def join
  value = @value.is_a?(monad_type) ? @value.unwrap(nil) : @value
  monad_type.unit(value)
end