Class: Monadic::Maybe

Inherits:
Monad show all
Defined in:
lib/monadic/maybe.rb

Direct Known Subclasses

Just, Nothing

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Monad

#==, #bind, #fetch, #initialize, #join, #map, #to_s

Constructor Details

This class inherits a constructor from Monadic::Monad

Class Method Details

.unit(value) ⇒ Object



3
4
5
6
# File 'lib/monadic/maybe.rb', line 3

def self.unit(value)
  return Nothing if value.nil? || (value.respond_to?(:empty?) && value.empty?)
  return Just.new(value)
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/monadic/maybe.rb', line 12

def empty?
  @value.respond_to?(:empty?) && @value.empty?
end

#select(proc = nil, &block) ⇒ Object



22
23
24
25
26
# File 'lib/monadic/maybe.rb', line 22

def select(proc = nil, &block)
  return Maybe(@value.select(&block)) if @value.is_a?(::Enumerable)
  return Nothing unless (proc || block).call(@value)
  return self
end

#to_aryObject Also known as: to_a



16
17
18
19
# File 'lib/monadic/maybe.rb', line 16

def to_ary
  return [@value].flatten if @value.respond_to? :flatten
  return [@value]
end

#truly?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/monadic/maybe.rb', line 28

def truly?
  @value == true
end