class Dry::Monads::Result::Success

Represents a value of a successful operation.

@api public

Public Class Methods

[](*value) click to toggle source

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
new(value) click to toggle source

@param value [Object] a value of a successful operation

Calls superclass method
# File lib/dry/monads/result.rb, line 80
def initialize(value)
  super()
  @value = value
end

Public Instance Methods

alt_map(_ = nil) click to toggle source

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
either(f, _) click to toggle source

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
failure?() click to toggle source

Returns false

# File lib/dry/monads/result.rb, line 93
def failure?
  false
end
flip() click to toggle source

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
fmap(...) click to toggle source

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
inspect()
Alias for: to_s
result(_, f) click to toggle source

Apply the second function to value.

@api public

# File lib/dry/monads/result.rb, line 88
def result(_, f)
  f.(@value)
end
success?() click to toggle source

Returns true

# File lib/dry/monads/result.rb, line 98
def success?
  true
end
to_maybe() click to toggle source

@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
to_s() click to toggle source

@return [String]

# File lib/dry/monads/result.rb, line 128
def to_s
  if Unit.equal?(@value)
    "Success()"
  else
    "Success(#{@value.inspect})"
  end
end
Also aliased as: inspect
to_validated() click to toggle source

Transforms to Validated

@return [Validated::Valid]

# File lib/dry/monads/validated.rb, line 292
def to_validated
  Validated::Valid.new(value!)
end