class Result

Result can hold a success value in an `Ok` or and error in an `Err`

Constants

VERSION

Attributes

err_description[R]
err_value[R]
ok[R]

Public Class Methods

err(description:, value:) click to toggle source
# File lib/rresult.rb, line 15
def self.err(description:, value:)
    raise ArgumentError, 'The error description should be a symbol' unless description.is_a? Symbol

    new(nil, description, value, :err)
end
ok(value) click to toggle source
# File lib/rresult.rb, line 11
def self.ok(value)
    new(value, nil, nil, :ok)
end

Private Class Methods

new(ok_val, err_description, err_value, type) click to toggle source
# File lib/rresult.rb, line 7
def initialize(ok_val, err_description, err_value, type)
    @ok, @err_description, @err_value, @type = ok_val, err_description, err_value, type
end

Public Instance Methods

==(other) click to toggle source
# File lib/rresult.rb, line 104
def ==(other)
    @ok == other.ok && @err_description == other.err_description && @err_value == err_value
end
===(other) click to toggle source
# File lib/rresult.rb, line 108
def ===(other)
    self == other
end
and(other_res) click to toggle source
# File lib/rresult.rb, line 58
def and(other_res)
    err? ? self : other_res
end
and_then() { |ok| ... } click to toggle source
# File lib/rresult.rb, line 62
def and_then
    return self if err?

    new_res = yield @ok
    raise "The provided block should return a Result, not a #{new_res.class}" unless new_res.is_a? Result

    self.and new_res
end
contains?(val) click to toggle source
# File lib/rresult.rb, line 29
def contains?(val)
    @ok == val
end
Also aliased as: contains
contains_err?(description, value) click to toggle source
# File lib/rresult.rb, line 33
def contains_err?(description, value)
    @err_description == description && @err_value == value
end
Also aliased as: contains_err
err?() click to toggle source
# File lib/rresult.rb, line 25
def err?
    @type == :err
end
Also aliased as: is_err
inspect() click to toggle source
# File lib/rresult.rb, line 112
def inspect
    ok? ? "Result::Ok(#{@ok.inspect})" : "Result::Err(#{@err_description.inspect}, #{@err_value.inspect})"
end
map() { |ok)| ... } click to toggle source
# File lib/rresult.rb, line 37
def map
    return self if err?

    Result.ok(yield @ok)
end
map_err() { |err_description, err_value| ... } click to toggle source
# File lib/rresult.rb, line 51
def map_err
    return self if ok?

    descr, val = yield @err_description, @err_value
    Result.err(description: descr, value: val)
end
map_or(val) { |ok| ... } click to toggle source
# File lib/rresult.rb, line 43
def map_or(val)
    err? ? val : yield(@ok)
end
map_or_else(map:, or_else:) click to toggle source
# File lib/rresult.rb, line 47
def map_or_else(map:, or_else:)
    err? ? or_else.call(@err_description, @err_value) : map.call(@ok)
end
ok?() click to toggle source
# File lib/rresult.rb, line 21
def ok?
    @type == :ok
end
Also aliased as: is_ok
or(other_res) click to toggle source
# File lib/rresult.rb, line 71
def or(other_res)
    ok? ? self : other_res
end
or_else() { |err_description, err_value| ... } click to toggle source
# File lib/rresult.rb, line 75
def or_else
    return self if ok?

    new_res = yield @err_description, @err_value
    raise "The provided block should return a Result, not a #{new_res.class}" unless new_res.is_a? Result

    self.or new_res
end
unwrap() click to toggle source
# File lib/rresult.rb, line 92
def unwrap
    raise unwrap_runtime_error_msg if err?

    @ok
end
unwrap_err() click to toggle source
# File lib/rresult.rb, line 98
def unwrap_err
    raise unwrap_err_runtime_error_msg if ok?

    [@err_description, @err_value]
end
unwrap_or(ok_value) click to toggle source
# File lib/rresult.rb, line 84
def unwrap_or(ok_value)
    err? ? ok_value : @ok
end
unwrap_or_else() { |err_description, err_value| ... } click to toggle source
# File lib/rresult.rb, line 88
def unwrap_or_else
    err? ? yield(@err_description, @err_value) : @ok
end

Private Instance Methods

contains(val)
Alias for: contains?
contains_err(description, value)
Alias for: contains_err?
is_err()
Alias for: err?
is_ok()
Alias for: ok?
unwrap_err_runtime_error_msg() click to toggle source
# File lib/rresult.rb, line 122
def unwrap_err_runtime_error_msg
    "\nCan't unwrap_err a Result::Ok\n    #{@ok}"
end
unwrap_runtime_error_msg() click to toggle source
# File lib/rresult.rb, line 118
def unwrap_runtime_error_msg
    "\nCan't unwrap a Result::Err\n    #{@err_description}\n    #{@err_value}"
end