Class: Muina::Maybe Abstract

Inherits:
Object
  • Object
show all
Extended by:
T::Generic, T::Helpers, T::Sig
Defined in:
lib/muina/maybe.rb,
lib/muina/maybe/none.rb,
lib/muina/maybe/some.rb

Overview

This class is abstract.

Direct Known Subclasses

None, Some

Defined Under Namespace

Classes: None, Some

Constant Summary collapse

UnwrappingError =

Raised when trying to unwrap a None value

Class.new(Error)
Elem =
type_member
ElemT =
type_template { { upper: Object } }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.noneNone

Returns a None, a safer alternative to nil.

Returns:



39
40
41
# File 'lib/muina/maybe.rb', line 39

def none
  None.__send__(:new)
end

.return(value) ⇒ Some<Elem> Also known as: some

Returns a Some wrapping the provided value.

Parameters:

  • value (Elem)

    a value to wrap around a Some variant.

Returns:



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

def return(value)
  Some.__send__(:new, T.unsafe(value))
end

Instance Method Details

#==(other) ⇒ Boolean

Returns true if both instances are of the same variant, and the contained values are equal in the case of Some.

Parameters:

Returns:

  • (Boolean)


196
197
# File 'lib/muina/maybe.rb', line 196

def ==(other)
end

#and_then {|value| ... } ⇒ self

Runs the provided block only if instance is of the Some variant, yielding the contained value. Always returns self.

Yield Parameters:

  • value (Elem)

    the contained value is passed to the block

Returns:

  • (self)


113
114
# File 'lib/muina/maybe.rb', line 113

def and_then(&_blk)
end

#bind {|value| ... } ⇒ Maybe

If instance is of the Some variant, it yields the contained value to the block and it returns the return value of the block; if it is of the None variant, it returns itself.

Yield Parameters:

  • value (Elem)

    the contained value is passed to the block

Yield Returns:

Returns:



172
173
# File 'lib/muina/maybe.rb', line 172

def bind(&_blk)
end

#bind_none { ... } ⇒ Maybe

If instance is of the None variant, it runs the provided block and it returns its return value; if it is of the Some variant, it returns itself.

Yields:

Yield Returns:

Returns:



187
188
# File 'lib/muina/maybe.rb', line 187

def bind_none(&blk)
end

#map {|value| ... } ⇒ Maybe<yield>, None

If instance is of the Some variant, it passes the contained value to the block and returns a new Some instance containing the return value of the block; if instance is of the None variant, it returns itself.

Yield Parameters:

  • value (Elem)

    the contained value is passed to the block

Yield Returns:

  • (Object)

Returns:



140
141
# File 'lib/muina/maybe.rb', line 140

def map(&_blk)
end

#map_none { ... } ⇒ Maybe<yield>, self

If instance is of the Some variant, it returns itself; if instance is of the None variant it runs the block and returns a new Some instance containing the return value of the block.

Yields:

Yield Returns:

  • (Object)

Returns:



155
156
# File 'lib/muina/maybe.rb', line 155

def map_none(&blk)
end

#none?true, false

Returns true if instance is of the None variant, or false if it is of the Some variant.

Returns:

  • (true)

    if instance is of the None variant

  • (false)

    if instance is of the Some variant



59
60
# File 'lib/muina/maybe.rb', line 59

def none?
end

#or_else { ... } ⇒ self

Runs the provided block only if instance is of the None variant, yielding no value to the block. Always returns self.

Yields:

Returns:

  • (self)


123
124
# File 'lib/muina/maybe.rb', line 123

def or_else(&_blk)
end

#some?true, false

Returns true if instance is of the Some variant, or false if it is of the None variant.

Returns:

  • (true)

    if instance is of the Some variant

  • (false)

    if instance is of the None variant



50
51
# File 'lib/muina/maybe.rb', line 50

def some?
end

#value!Elem

Returns the contained value if instance is of the Some variant, or raises UnwrappingError if it is of the None variant.

Returns:

Raises:



68
69
# File 'lib/muina/maybe.rb', line 68

def value!
end

#value_or(default) ⇒ Elem, Object

Returns the contained value if instance is of the Some variant, or the provided default value if it is of the None variant.

Parameters:

  • default (Object)

    the value to be used if the instance is of the None variant

Returns:



82
83
# File 'lib/muina/maybe.rb', line 82

def value_or(default)
end

#value_or_nilElem?

Returns the contained value if instance is of the Some variant, or nil if it is of the None variant.

Returns:



103
104
# File 'lib/muina/maybe.rb', line 103

def value_or_nil
end

#value_or_yield(&blk) ⇒ Elem, yield

Returns the contained value if instance is of the Some variant, or runs the provided block and returns its result if it is of the None variant.

Yield Returns:

  • (Object)

Returns:



95
96
# File 'lib/muina/maybe.rb', line 95

def value_or_yield(&blk)
end