class Monads::Result

Constants

FAILURE_TRIGGER

Public Class Methods

unit(value) click to toggle source
unit

a -> M a

# File lib/ruby-monads/result.rb, line 8
def self.unit(value)
  if value.is_a?(FAILURE_TRIGGER) || value.is_a?(Failure)
    Failure.new(value)
  else
    Success.new(value)
  end
rescue => error
  Failure.new(error)
end

Public Instance Methods

bind(&block) click to toggle source
bind

(a -> M b) -> M a -> M b

# File lib/ruby-monads/result.rb, line 19
def bind(&block)
  return self if is_a?(Failure)

  begin
    ensure_monadic_result(&block).call
  rescue FAILURE_TRIGGER => error
    Failure.new(error)
  end
end
unwrap(default_value = @value) click to toggle source
unwrap

a -> M a -> a

# File lib/ruby-monads/result.rb, line 30
def unwrap(default_value = @value)
  is_a?(Failure) ? default_value : @value
end

Private Instance Methods

monad_type() click to toggle source
# File lib/ruby-monads/result.rb, line 36
def monad_type
  Result
end