Class: Optional

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Monad

#within

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



30
31
32
33
34
# File 'lib/maybe.rb', line 30

def method_missing(*args, &block)
  and_then do |value|
    Optional.new(value.public_send(*args, &block))
  end
end

Instance Attribute Details

#valueObject

Returns the value of attribute value

Returns:

  • (Object)

    the current value of value



21
22
23
# File 'lib/maybe.rb', line 21

def value
  @value
end

Instance Method Details

#and_then(&block) ⇒ Object



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

def and_then(&block)
  if value.nil?
    Optional.new(nil)
  else
    block.call(value)
  end
end