Class: Philiprehberger::Result::Ok
- Inherits:
-
Object
- Object
- Philiprehberger::Result::Ok
- Includes:
- Filterable, Tappable
- Defined in:
- lib/philiprehberger/result/ok.rb
Overview
Represents a successful result containing a value.
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#contains?(other) ⇒ Boolean
Check whether this Ok contains the given value (using ==).
-
#contains_err?(_other) ⇒ Boolean
Always false on Ok (there is no error to compare).
-
#deconstruct ⇒ Array
Pattern matching support via ‘in Ok`.
-
#deconstruct_keys(_keys) ⇒ Hash
Pattern matching support via ‘in Ok(value:)`.
-
#err? ⇒ Boolean
False.
-
#flat_map {|value| ... } ⇒ Ok, Err
(also: #and_then)
Chain a result-returning operation.
-
#initialize(value) ⇒ Ok
constructor
A new instance of Ok.
-
#map {|value| ... } ⇒ Ok
Transform the success value.
-
#map_err ⇒ Ok
Ignore the error transformation.
-
#map_or(_default) {|value| ... } ⇒ Object
Map the value with a fallback for Err.
-
#ok? ⇒ Boolean
True.
-
#or_else ⇒ Ok
Return self (no error to recover from).
-
#recover(_error_class = nil) ⇒ Object
Recovery is a no-op on Ok.
-
#to_h ⇒ Hash
Serialize to a hash.
-
#to_maybe ⇒ Object
Convert to a Maybe-like value: the success value, or nil for Err.
- #to_s ⇒ Object (also: #inspect)
-
#unwrap! ⇒ Object
Return the value.
-
#unwrap_err! ⇒ Object
Raise because Ok has no error to unwrap.
-
#unwrap_or(_default) ⇒ Object
Return the value or ignore the fallback.
-
#zip(other) ⇒ Object
Combine two Ok results into Ok([a, b]).
Methods included from Filterable
Methods included from Tappable
Constructor Details
#initialize(value) ⇒ Ok
Returns a new instance of Ok.
13 14 15 |
# File 'lib/philiprehberger/result/ok.rb', line 13 def initialize(value) @value = value end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
10 11 12 |
# File 'lib/philiprehberger/result/ok.rb', line 10 def value @value end |
Instance Method Details
#==(other) ⇒ Object
141 142 143 |
# File 'lib/philiprehberger/result/ok.rb', line 141 def ==(other) other.is_a?(Ok) && other.value == @value end |
#contains?(other) ⇒ Boolean
Check whether this Ok contains the given value (using ==).
107 108 109 |
# File 'lib/philiprehberger/result/ok.rb', line 107 def contains?(other) @value == other end |
#contains_err?(_other) ⇒ Boolean
Always false on Ok (there is no error to compare).
115 116 117 |
# File 'lib/philiprehberger/result/ok.rb', line 115 def contains_err?(_other) false end |
#deconstruct ⇒ Array
Pattern matching support via ‘in Ok`.
129 130 131 |
# File 'lib/philiprehberger/result/ok.rb', line 129 def deconstruct [@value] end |
#deconstruct_keys(_keys) ⇒ Hash
Pattern matching support via ‘in Ok(value:)`.
137 138 139 |
# File 'lib/philiprehberger/result/ok.rb', line 137 def deconstruct_keys(_keys) { value: @value } end |
#err? ⇒ Boolean
Returns false.
21 |
# File 'lib/philiprehberger/result/ok.rb', line 21 def err? = false |
#flat_map {|value| ... } ⇒ Ok, Err Also known as: and_then
Chain a result-returning operation.
35 36 37 |
# File 'lib/philiprehberger/result/ok.rb', line 35 def flat_map(&block) block.call(@value) end |
#map {|value| ... } ⇒ Ok
Transform the success value.
27 28 29 |
# File 'lib/philiprehberger/result/ok.rb', line 27 def map(&block) Ok.new(block.call(@value)) end |
#map_err ⇒ Ok
Ignore the error transformation.
61 62 63 |
# File 'lib/philiprehberger/result/ok.rb', line 61 def map_err self end |
#map_or(_default) {|value| ... } ⇒ Object
Map the value with a fallback for Err.
87 88 89 |
# File 'lib/philiprehberger/result/ok.rb', line 87 def map_or(_default, &block) block.call(@value) end |
#ok? ⇒ Boolean
Returns true.
18 |
# File 'lib/philiprehberger/result/ok.rb', line 18 def ok? = true |
#or_else ⇒ Ok
Return self (no error to recover from).
68 69 70 |
# File 'lib/philiprehberger/result/ok.rb', line 68 def or_else self end |
#recover(_error_class = nil) ⇒ Object
Recovery is a no-op on Ok
92 93 94 |
# File 'lib/philiprehberger/result/ok.rb', line 92 def recover(_error_class = nil) self end |
#to_h ⇒ Hash
Serialize to a hash.
122 123 124 |
# File 'lib/philiprehberger/result/ok.rb', line 122 def to_h { ok: @value } end |
#to_maybe ⇒ Object
Convert to a Maybe-like value: the success value, or nil for Err.
99 100 101 |
# File 'lib/philiprehberger/result/ok.rb', line 99 def to_maybe @value end |
#to_s ⇒ Object Also known as: inspect
145 146 147 |
# File 'lib/philiprehberger/result/ok.rb', line 145 def to_s "Ok(#{@value.inspect})" end |
#unwrap! ⇒ Object
Return the value.
49 |
# File 'lib/philiprehberger/result/ok.rb', line 49 def unwrap! = @value |
#unwrap_err! ⇒ Object
Raise because Ok has no error to unwrap.
54 55 56 |
# File 'lib/philiprehberger/result/ok.rb', line 54 def unwrap_err! raise UnwrapError, "Called unwrap_err! on Ok(#{@value.inspect})" end |
#unwrap_or(_default) ⇒ Object
Return the value or ignore the fallback.
43 |
# File 'lib/philiprehberger/result/ok.rb', line 43 def unwrap_or(_default) = @value |