Class: Muina::Maybe::Some
- Inherits:
-
Muina::Maybe
- Object
- Muina::Maybe
- Muina::Maybe::Some
- Defined in:
- lib/muina/maybe/some.rb
Overview
rubocop:disable Metrics/ClassLength
Constant Summary collapse
- Elem =
type_member { { upper: Object } }
- ElemT =
type_template { { upper: Object } }
Constants inherited from Muina::Maybe
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Returns
true
if both instances are of the same variant, and the contained values are equal in the case of Some. -
#and_then {|value| ... } ⇒ self
Runs the provided block only if instance is of the Some variant, yielding the contained value.
- #bind {|value| ... } ⇒ Maybe
- #bind_none { ... } ⇒ Maybe
-
#initialize(value) ⇒ Some
constructor
rubocop:disable Lint/MissingSuper.
- #map {|value| ... } ⇒ Maybe<yield>, None
- #map_none { ... } ⇒ Maybe<yield>, self
- #none? ⇒ true, false
-
#or_else { ... } ⇒ self
Runs the provided block only if instance is of the None variant, yielding no value to the block.
- #some? ⇒ Boolean
-
#value! ⇒ Elem
Returns the contained value if instance is of the Some variant, or raises UnwrappingError if it is of the None variant.
- #value_or(_default) ⇒ Object
- #value_or_nil ⇒ Elem?
- #value_or_yield(&_blk) ⇒ Elem, yield
Methods inherited from Muina::Maybe
Constructor Details
#initialize(value) ⇒ Some
rubocop:disable Lint/MissingSuper
19 20 21 22 |
# File 'lib/muina/maybe/some.rb', line 19 def initialize(value) # rubocop:disable Lint/MissingSuper @value = value freeze 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 Muina::Maybe::Some.
127 128 129 130 |
# File 'lib/muina/maybe/some.rb', line 127 def ==(other) self.class == other.class && value! == other.value! end |
#and_then {|value| ... } ⇒ self
Runs the provided block only if instance is of the Muina::Maybe::Some variant, yielding the contained value. Always returns self
.
70 71 72 73 |
# File 'lib/muina/maybe/some.rb', line 70 def and_then(&_blk) yield(@value) self end |
#bind {|value| ... } ⇒ Maybe
If instance is of the Muina::Maybe::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.
111 112 113 |
# File 'lib/muina/maybe/some.rb', line 111 def bind(&_blk) yield(@value) 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 Muina::Maybe::Some variant, it returns itself.
121 122 123 |
# File 'lib/muina/maybe/some.rb', line 121 def bind_none(&_blk) self end |
#map {|value| ... } ⇒ Maybe<yield>, None
If instance is of the Muina::Maybe::Some variant, it passes the contained value to the block and returns a new Muina::Maybe::Some instance containing the return value of the block; if instance is of the None variant, it returns itself.
89 90 91 |
# File 'lib/muina/maybe/some.rb', line 89 def map(&_blk) Maybe.return yield(@value) end |
#map_none { ... } ⇒ Maybe<yield>, self
If instance is of the Muina::Maybe::Some variant, it returns itself; if instance is of the None variant it runs the block and returns a new Muina::Maybe::Some instance containing the return value of the block.
99 100 101 |
# File 'lib/muina/maybe/some.rb', line 99 def map_none(&_blk) self end |
#none? ⇒ true, false
Returns true
if instance is of the None variant, or false
if it is of the Muina::Maybe::Some variant.
32 33 34 |
# File 'lib/muina/maybe/some.rb', line 32 def none? false 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
.
77 78 79 |
# File 'lib/muina/maybe/some.rb', line 77 def or_else(&_blk) self end |
#some? ⇒ Boolean
26 27 28 |
# File 'lib/muina/maybe/some.rb', line 26 def some? true end |
#value! ⇒ Elem
Returns the contained value if instance is of the Muina::Maybe::Some variant, or raises UnwrappingError if it is of the None variant.
38 39 40 |
# File 'lib/muina/maybe/some.rb', line 38 def value! @value end |
#value_or(_default) ⇒ Object
48 49 50 |
# File 'lib/muina/maybe/some.rb', line 48 def value_or(_default) @value end |
#value_or_nil ⇒ Elem?
Returns the contained value if instance is of the Muina::Maybe::Some variant, or nil
if it is of the None variant.
64 65 66 |
# File 'lib/muina/maybe/some.rb', line 64 def value_or_nil @value end |
#value_or_yield(&_blk) ⇒ Elem, yield
Returns the contained value if instance is of the Muina::Maybe::Some variant, or runs the provided block and returns its result if it is of the None variant.
58 59 60 |
# File 'lib/muina/maybe/some.rb', line 58 def value_or_yield(&_blk) @value end |