Class: MonadOxide::Ok
Overview
‘Ok’ represents a success ‘Result’. For most operations, ‘Ok’ will perform some operation. Exceptions raised during calls to ‘Ok’ will coerce the chain into ‘Err’, which generally causes execution to fall through the entire chain.
Instance Method Summary collapse
-
#and_then(f = nil) { ... } ⇒ Ok<B> | Err<C>
Invokes ‘f’ or the block with the data and returns the Result returned from that.
-
#err? ⇒ Boolean
Identifies that this is not an ‘Err`.
-
#initialize(data) ⇒ Ok
constructor
Constructs an ‘Ok’ with the data provided.
-
#inspect_err(f = nil) { ... } ⇒ Ok
Falls through.
-
#inspect_ok(f = nil) { ... } ⇒ Result<A>
Applies ‘f’ or the block over the data and returns the same ‘Ok’.
-
#map(f = nil) { ... } ⇒ Result<B>
Applies ‘f’ or the block over the data and returns a new new ‘Ok’ with the returned value.
-
#map_err(f = nil) { ... } ⇒ Result<A>
This is a no-op for Ok.
-
#ok? ⇒ Boolean
Identifies that this is an ‘Ok`.
-
#or_else(f = nil) { ... } ⇒ Result<A>
The Err equivalent to Ok#and_then.
-
#unwrap ⇒ A
Dangerously access the ‘Ok’ data.
-
#unwrap_err ⇒ E
Dangerously access the ‘Err’ data.
-
#unwrap_err_or_else(f = nil) { ... } ⇒ B
Safely unwrap the ‘Result`.
-
#unwrap_or(_x) ⇒ A
Safely unwrap the ‘Result`.
-
#unwrap_or_else(_f = nil, &_block) ⇒ B
Safely unwrap the ‘Result`.
Methods inherited from Result
Constructor Details
#initialize(data) ⇒ Ok
Constructs an ‘Ok’ with the data provided.
13 14 15 |
# File 'lib/ok.rb', line 13 def initialize(data) @data = data end |
Instance Method Details
#and_then(f = nil) { ... } ⇒ Ok<B> | Err<C>
Invokes ‘f’ or the block with the data and returns the Result returned from that. Exceptions raised during ‘f’ or the block will return an ‘Err<Exception>’. The return type is enforced.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ok.rb', line 28 def and_then(f=nil, &block) begin r = (f || block).call(@data) # Enforce that we always get a Result. Without a Result, coerce to an # Err. if !r.kind_of?(Result) raise ResultReturnExpectedError.new(r) else r end rescue => e Err.new(e) end end |
#err? ⇒ Boolean
Identifies that this is not an ‘Err`. For counterparts:
50 51 52 |
# File 'lib/ok.rb', line 50 def err?() false end |
#inspect_err(f = nil) { ... } ⇒ Ok
Falls through. @see Result#inspect_err for how this is handled in either Result case, and @see Err.inspect_err for how this is handled in the Err case.
61 62 63 |
# File 'lib/ok.rb', line 61 def inspect_err(f=nil, &block) self end |
#inspect_ok(f = nil) { ... } ⇒ Result<A>
Applies ‘f’ or the block over the data and returns the same ‘Ok’. No changes are applied. This is ideal for logging. Exceptions raised during these transformations will return an ‘Err’ with the Exception.
74 75 76 77 78 79 80 81 |
# File 'lib/ok.rb', line 74 def inspect_ok(f=nil, &block) begin (f || block).call(@data) self rescue => e Err.new(e) end end |
#map(f = nil) { ... } ⇒ Result<B>
Applies ‘f’ or the block over the data and returns a new new ‘Ok’ with the returned value.
93 94 95 96 97 98 99 |
# File 'lib/ok.rb', line 93 def map(f=nil, &block) begin self.class.new((f || block).call(@data)) rescue => e Err.new(e) end end |
#map_err(f = nil) { ... } ⇒ Result<A>
This is a no-op for Ok. @see Err#map_err.
106 107 108 |
# File 'lib/ok.rb', line 106 def map_err(f=nil, &block) self end |
#ok? ⇒ Boolean
Identifies that this is an ‘Ok`. For counterparts:
117 118 119 |
# File 'lib/ok.rb', line 117 def ok?() true end |
#or_else(f = nil) { ... } ⇒ Result<A>
The Err equivalent to Ok#and_then. This is a no-op for Ok. @see Err#or_else.
127 128 129 |
# File 'lib/ok.rb', line 127 def or_else(f=nil, &block) self end |
#unwrap ⇒ A
Dangerously access the ‘Ok’ data. If this is an ‘Err’, an exception will be raised. It is recommended to use this for tests only.
135 136 137 |
# File 'lib/ok.rb', line 135 def unwrap() @data end |
#unwrap_err ⇒ E
Dangerously access the ‘Err’ data. If this is an ‘Ok’, an exception will be raised. It is recommended to use this for tests only.
143 144 145 146 147 |
# File 'lib/ok.rb', line 143 def unwrap_err() raise UnwrapError.new( "#{self.class} with #{@data.inspect} could not be unwrapped as an Err.", ) end |
#unwrap_err_or_else(f = nil) { ... } ⇒ B
Safely unwrap the ‘Result`. In the case of `Ok`, this uses the provided function to produce a value.
158 159 160 |
# File 'lib/ok.rb', line 158 def unwrap_err_or_else(f=nil, &block) (f || block).call() end |
#unwrap_or(_x) ⇒ A
Safely unwrap the ‘Result`. In the case of `Ok`, this returns the data in the Ok.
168 169 170 |
# File 'lib/ok.rb', line 168 def unwrap_or(_x) @data end |
#unwrap_or_else(_f = nil, &_block) ⇒ B
Safely unwrap the ‘Result`. In the case of `Ok`, this returns the wrapped value.
179 180 181 |
# File 'lib/ok.rb', line 179 def unwrap_or_else(_f=nil, &_block) @data end |