class Dry::Monads::Result::Success
Represents a value of a successful operation.
@api public
Public Class Methods
Shortcut for Success()
@example include Dry::Monads[:result] def call Success[200, {}, ['ok']] # => Success([200, {}, ['ok']]) end
@api public
# File lib/dry/monads/result.rb, line 73 def self.[](*value) new(value) end
@param value [Object] a value of a successful operation
# File lib/dry/monads/result.rb, line 80 def initialize(value) super() @value = value end
Public Instance Methods
Ignores values and returns self, see {Failure#alt_map}
@return [Result::Success]
# File lib/dry/monads/result.rb, line 147 def alt_map(_ = nil) self end
Returns result of applying first function to the internal value.
@example
Dry::Monads.Success(1).either(-> x { x + 1 }, -> x { x + 2 }) # => 2
@param f [#call] Function to apply @param _ [#call] Ignored @return [Any] Return value of ‘f`
# File lib/dry/monads/result.rb, line 123 def either(f, _) f.(success) end
Returns false
# File lib/dry/monads/result.rb, line 93 def failure? false end
Transforms to a Failure
instance
@return [Result::Failure]
# File lib/dry/monads/result.rb, line 140 def flip Failure.new(@value, RightBiased::Left.trace_caller) end
Does the same thing as bind
except it also wraps the value in an instance of Result::Success
monad. This allows for easier chaining of calls.
@example
Dry::Monads.Success(4).fmap(&:succ).fmap(->(n) { n**2 }) # => Success(25)
@param args [Array<Object>] arguments will be transparently passed through to bind
@return [Result::Success]
# File lib/dry/monads/result.rb, line 111 def fmap(...) Success.new(bind(...)) end
Apply the second function to value.
@api public
# File lib/dry/monads/result.rb, line 88 def result(_, f) f.(@value) end
Returns true
# File lib/dry/monads/result.rb, line 98 def success? true end
@return [Maybe]
# File lib/dry/monads/maybe.rb, line 388 def to_maybe warn "Success(nil) transformed to None" if @value.nil? ::Dry::Monads::Maybe(@value) end
@return [String]
# File lib/dry/monads/result.rb, line 128 def to_s if Unit.equal?(@value) "Success()" else "Success(#{@value.inspect})" end end
Transforms to Validated
@return [Validated::Valid]
# File lib/dry/monads/validated.rb, line 292 def to_validated Validated::Valid.new(value!) end