Class: Dry::Monads::Maybe::Some

Inherits:
Dry::Monads::Maybe show all
Includes:
RightBiased::Right
Defined in:
lib/dry/monads/maybe.rb

Overview

Represents a value that is present, i.e. not nil.

Instance Method Summary collapse

Methods included from RightBiased::Right

#===, #apply, #bind, #discard, included, #or, #or_fmap, #tee, #value!, #value_or

Methods inherited from Dry::Monads::Maybe

#as_json, coerce, json_create, #monad, #none?, pure, #some?, #to_json, #to_maybe, #to_monad, to_proc

Methods included from Transformer

#fmap2, #fmap3

Constructor Details

#initialize(value = Undefined) ⇒ Some

Returns a new instance of Some.

Raises:

  • (ArgumentError)


91
92
93
94
# File 'lib/dry/monads/maybe.rb', line 91

def initialize(value = Undefined)
  raise ArgumentError, 'nil cannot be some' if value.nil?
  @value = Undefined.default(value, Unit)
end

Instance Method Details

#fmap(*args, &block) ⇒ Maybe::Some, Maybe::None

Does the same thing as #bind except it also wraps the value in an instance of Maybe::Some monad. This allows for easier chaining of calls.

Examples:

Dry::Monads.Some(4).fmap(&:succ).fmap(->(n) { n**2 }) # => Some(25)

Parameters:

  • args (Array<Object>)

    arguments will be transparently passed through to #bind

Returns:

  • (Maybe::Some, Maybe::None)

    Wrapped result, i.e. nil will be mapped to None, other values will be wrapped with Some



106
107
108
# File 'lib/dry/monads/maybe.rb', line 106

def fmap(*args, &block)
  self.class.coerce(bind(*args, &block))
end

#to_sString Also known as: inspect

Returns:

  • (String)


111
112
113
# File 'lib/dry/monads/maybe.rb', line 111

def to_s
  "Some(#{ @value.inspect })"
end