Class: R::Ok
- Inherits:
-
Object
- Object
- R::Ok
- Extended by:
- T::Generic, T::Helpers, T::Sig
- Includes:
- Result
- Defined in:
- lib/r/result.rb
Overview
Contains the success value.
Constant Summary collapse
- OkType =
The type of the success value.
type_member
- ErrType =
The type of the error value.
In the context of an R::Ok value, this is set to
T.noreturn
. This enables Sorbet to detect potential dead code paths. type_member { { fixed: T.noreturn } }
Class Method Summary collapse
-
.new(value) ⇒ T.all(T.attached_class, Ok[T.type_parameter(:T)])
Creates a new instance of Ok.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Returns
true
ifother
is Ok andself.ok == other.ok
. -
#and(res) ⇒ Result[T.type_parameter(:U), T.type_parameter(:F)]
Returns
res
. -
#and_then(&blk) {|@value| ... } ⇒ Result[T.type_parameter(:U), T.type_parameter(:F)]
Calls the block.
-
#err ⇒ nil
Returns
nil
. -
#err? ⇒ false
Returns
false
. -
#err_and?(&blk) ⇒ false
Returns
false
. -
#expect!(msg) ⇒ OkType
Returns the contained Ok value.
-
#expect_err!(msg) ⇒ T.noreturn
Raises an UnwrapFailedError.
-
#initialize(value) ⇒ void
constructor
Creates a new instance of Ok.
-
#inspect ⇒ String
Returns a string representation of
self
. - #map(&blk) ⇒ Ok[T.type_parameter(:U)]
-
#map_err(&blk) ⇒ Ok[OkType]
Leaves the value untouched.
-
#map_or(default, &blk) {|@value| ... } ⇒ T.type_parameter(:U)
Applies a function to the contained value.
- #map_or_else(default, &blk) {|@value| ... } ⇒ T.type_parameter(:U)
-
#ok ⇒ OkType
Returns the value.
-
#ok? ⇒ true
Returns
true
. -
#ok_and?(&blk) {|@value| ... } ⇒ Boolean
Returns
true
if value matches a predicate. -
#on_err(&blk) ⇒ T.self_type
Does nothing.
-
#on_ok(&blk) {|@value| ... } ⇒ T.self_type
Calls the provided block with the contained value.
-
#or(res) ⇒ Result[OkType, T.type_parameter(:F)]
Returns the Ok value of
self
. -
#or_else(&blk) ⇒ Result[OkType, T.type_parameter(:F)]
Returns the Ok value of
self
. -
#try?(&blk) ⇒ OkType
Returns the contained Ok value.
-
#unwrap! ⇒ OkType
Returns the contained Ok value.
-
#unwrap_err! ⇒ T.noreturn
Raises an UnwrapFailedError.
-
#unwrap_or(default) ⇒ OkType
Returns the contained Ok value.
-
#unwrap_or_else(&blk) ⇒ OkType
Returns the contained Ok value.
Constructor Details
#initialize(value) ⇒ void
Creates a new instance of R::Ok.
680 681 682 |
# File 'lib/r/result.rb', line 680 def initialize(value) @value = value end |
Class Method Details
Instance Method Details
#==(other) ⇒ Boolean
Returns true
if other
is R::Ok and self.ok == other.ok
.
634 635 636 637 638 639 640 641 |
# File 'lib/r/result.rb', line 634 def ==(other) case other when Ok other.ok == @value else false end end |
#and(res) ⇒ Result[T.type_parameter(:U), T.type_parameter(:F)]
Returns res
.
Arguments passed to and
are eagerly evaluated; if you are passing the result of a function call, it is
recommended to use #and_then, which is lazily evaluated.
944 945 946 |
# File 'lib/r/result.rb', line 944 def and(res) res end |
#and_then(&blk) {|@value| ... } ⇒ Result[T.type_parameter(:U), T.type_parameter(:F)]
Calls the block.
This function can be used for control flow based on Result values.
974 975 976 |
# File 'lib/r/result.rb', line 974 def and_then(&blk) yield(@value) end |
#err ⇒ nil
Returns nil
.
761 762 763 |
# File 'lib/r/result.rb', line 761 def err nil end |
#err? ⇒ false
Returns false
.
722 723 724 |
# File 'lib/r/result.rb', line 722 def err? false end |
#err_and?(&blk) ⇒ false
Returns false
.
735 736 737 |
# File 'lib/r/result.rb', line 735 def err_and?(&blk) false end |
#expect!(msg) ⇒ OkType
Returns the contained R::Ok value.
874 875 876 |
# File 'lib/r/result.rb', line 874 def expect!(msg) @value end |
#expect_err!(msg) ⇒ T.noreturn
Raises an UnwrapFailedError.
903 904 905 |
# File 'lib/r/result.rb', line 903 def expect_err!(msg) raise UnwrapFailedError.new(msg, @value) end |
#inspect ⇒ String
Returns a string representation of self
.
652 653 654 655 656 657 658 659 660 |
# File 'lib/r/result.rb', line 652 def inspect value_repr = case @value when Object @value.inspect else "<uninspectable value>" end "R.ok(#{value_repr})" end |
#map(&blk) ⇒ Ok[T.type_parameter(:U)]
777 778 779 |
# File 'lib/r/result.rb', line 777 def map(&blk) Ok.new(yield(@value)) end |
#map_err(&blk) ⇒ Ok[OkType]
Leaves the value untouched.
This function can be used to pass through a successful result while handling an error.
855 856 857 |
# File 'lib/r/result.rb', line 855 def map_err(&blk) self end |
#map_or(default, &blk) {|@value| ... } ⇒ T.type_parameter(:U)
Applies a function to the contained value.
Arguments passed to #map_or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use #map_or_else, which is lazily evaluated.
801 802 803 |
# File 'lib/r/result.rb', line 801 def map_or(default, &blk) yield(@value) end |
#map_or_else(default, &blk) {|@value| ... } ⇒ T.type_parameter(:U)
826 827 828 |
# File 'lib/r/result.rb', line 826 def map_or_else(default, &blk) yield(@value) end |
#ok? ⇒ true
Returns true
.
693 694 695 |
# File 'lib/r/result.rb', line 693 def ok? true end |
#ok_and?(&blk) {|@value| ... } ⇒ Boolean
Returns true
if value matches a predicate.
709 710 711 |
# File 'lib/r/result.rb', line 709 def ok_and?(&blk) yield(@value) end |
#on_err(&blk) ⇒ T.self_type
Does nothing.
Returns self
so this can be chained with #on_ok.
1136 1137 1138 |
# File 'lib/r/result.rb', line 1136 def on_err(&blk) self end |
#on_ok(&blk) {|@value| ... } ⇒ T.self_type
Calls the provided block with the contained value.
Returns self
so this can be chained with #on_err.
1118 1119 1120 1121 |
# File 'lib/r/result.rb', line 1118 def on_ok(&blk) yield(@value) self end |
#or(res) ⇒ Result[OkType, T.type_parameter(:F)]
1000 1001 1002 |
# File 'lib/r/result.rb', line 1000 def or(res) self end |
#or_else(&blk) ⇒ Result[OkType, T.type_parameter(:F)]
1034 1035 1036 |
# File 'lib/r/result.rb', line 1034 def or_else(&blk) self end |
#try?(&blk) ⇒ OkType
Returns the contained R::Ok value.
This method is similar to #unwrap_or_else, but in case of an Err value, the block must short-circuit by
either calling return
(which will return from the enclosing method) or raising an exception.
This is useful to make error handling less tedious when dealing with many methods returning results.
1101 1102 1103 |
# File 'lib/r/result.rb', line 1101 def try?(&blk) @value end |
#unwrap! ⇒ OkType
Returns the contained R::Ok value.
887 888 889 |
# File 'lib/r/result.rb', line 887 def unwrap! @value end |
#unwrap_err! ⇒ T.noreturn
Raises an UnwrapFailedError.
918 919 920 |
# File 'lib/r/result.rb', line 918 def unwrap_err! raise UnwrapFailedError.new("called `Result#unwrap_err!` on an `Ok` value", @value) end |
#unwrap_or(default) ⇒ OkType
Returns the contained R::Ok value.
Arguments passed to unwrap_or
are eagerly evaluated; if you are passing the result of a function call, it is
recommended to use #unwrap_or_else, which is lazily evaluated.
1057 1058 1059 |
# File 'lib/r/result.rb', line 1057 def unwrap_or(default) @value end |