Class: Dry::Monads::Maybe::None

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

Overview

Represents an absence of a value, i.e. the value nil.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RightBiased::Left

#and, #apply, #bind, #deconstruct_keys, #discard, #flatten, #fmap, #tee, trace_caller, #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(trace = RightBiased::Left.trace_caller) ⇒ None

Returns a new instance of None.



212
213
214
215
# File 'lib/dry/monads/maybe.rb', line 212

def initialize(trace = RightBiased::Left.trace_caller)
  super()
  @trace = trace
end

Instance Attribute Details

#instanceObject (readonly)



195
196
197
# File 'lib/dry/monads/maybe.rb', line 195

def instance
  @instance
end

#traceString (readonly)

Line where the value was constructed

Returns:

  • (String)


210
211
212
# File 'lib/dry/monads/maybe.rb', line 210

def trace
  @trace
end

Instance Method Details

#deconstructObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Pattern matching

Examples:

case Some(:foo)
in Some(Integer) then ...
in Some(:bar) then ...
in None() then ...
end


280
281
282
# File 'lib/dry/monads/maybe.rb', line 280

def deconstruct
  EMPTY_ARRAY
end

#eql?(other) ⇒ Boolean Also known as: ==

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


260
261
262
# File 'lib/dry/monads/maybe.rb', line 260

def eql?(other)
  other.is_a?(None)
end

#filter(_ = Undefined) ⇒ Maybe::None

Returns:

See Also:



287
288
289
# File 'lib/dry/monads/maybe.rb', line 287

def filter(_ = Undefined)
  self
end

#hashObject



266
267
268
# File 'lib/dry/monads/maybe.rb', line 266

def hash
  None.instance.object_id
end

#or(*args) ⇒ Object

If a block is given passes internal value to it and returns the result, otherwise simply returns the parameter val.

Examples:

Dry::Monads.None.or('no value') # => "no value"
Dry::Monads.None.or { Time.now } # => current time

Parameters:

  • args (Array<Object>)

    if no block given the first argument will be returned otherwise arguments will be transparently passed to the block

Returns:

  • (Object)


231
232
233
234
235
236
237
# File 'lib/dry/monads/maybe.rb', line 231

def or(*args)
  if block_given?
    yield(*args)
  else
    args[0]
  end
end

#or_fmapMaybe::Some, Maybe::None

A lifted version of ‘#or`. Applies `Maybe.coerce` to the passed value or to the block result.

Examples:

Dry::Monads.None.or_fmap('no value') # => Some("no value")
Dry::Monads.None.or_fmap { Time.now } # => Some(current time)

Parameters:

  • args (Array<Object>)

    arguments will be passed to the underlying ‘#or` call

Returns:

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

    Lifted ‘#or` result, i.e. nil will be mapped to None, other values will be wrapped with Some



249
250
251
# File 'lib/dry/monads/maybe.rb', line 249

def or_fmap(...)
  Maybe.coerce(self.or(...))
end

#to_result(fail = Unit) ⇒ Failure<Any>

Converts to Failure(fallback_value)

Parameters:

  • fail (#call) (defaults to: Unit)

    Fallback value

  • block (Proc)

    Fallback block

Returns:



419
420
421
422
423
424
425
# File 'lib/dry/monads/result.rb', line 419

def to_result(fail = Unit)
  if block_given?
    Result::Failure.new(yield)
  else
    Result::Failure.new(fail)
  end
end

#to_sString Also known as: inspect

Returns:

  • (String)


254
255
256
# File 'lib/dry/monads/maybe.rb', line 254

def to_s
  "None"
end