Class: MonadOxide::None

Inherits:
Option
  • Object
show all
Defined in:
lib/none.rb

Overview

‘None’ represents the absence of a value in an ‘Option’.

Any methods in Option that would process a present value (Some) will fall through with None instances.

Instance Method Summary collapse

Methods inherited from Option

#match

Constructor Details

#initializeNone

Create a None.



16
17
# File 'lib/none.rb', line 16

def initialize()
end

Instance Method Details

#and_then(f = nil) { ... } ⇒ None

Falls through. @see Option#and_then for how this is handled in either Option case, and @see Some#and_then for how this is handled in the Some case.

Parameters:

  • f (Proc) (defaults to: nil)

    Optional Proc - ignored.

Yields:

  • An ignored block.

Returns:

  • (None)

    This None.



26
27
28
# File 'lib/none.rb', line 26

def and_then(f=nil, &block)
  self
end

#inspect_none(f = nil) { ... } ⇒ Option

Applies ‘f’ or the block and returns the same ‘None’. No changes are applied. This is ideal for logging.

Parameters:

  • f (Proc<B>) (defaults to: nil)

    The function to call. Could be a block instead. Takes nothing, the return is ignored.

Yields:

  • Will yield a block that takes nothing, the return is ignored. Same as ‘f’ parameter.

Returns:



49
50
51
52
# File 'lib/none.rb', line 49

def inspect_none(f=nil, &block)
  (f || block).call()
  self
end

#inspect_some(f = nil) { ... } ⇒ None

Falls through. @see Option#inspect_some for how this is handled in either Option case, and @see Some#inspect_some for how this is handled in the Some case.

Parameters:

  • f (Proc) (defaults to: nil)

    Optional Proc - ignored.

Yields:

  • An ignored block.

Returns:

  • (None)

    This None.



37
38
39
# File 'lib/none.rb', line 37

def inspect_some(f=nil, &block)
  self
end

#map(f = nil) { ... } ⇒ None

Falls through. @see Option#map for how this is handled in either Option case, and @see Some#map for how this is handled in the Some case.

Parameters:

  • f (Proc) (defaults to: nil)

    Optional Proc - ignored.

Yields:

  • An ignored block.

Returns:

  • (None)

    This None.



61
62
63
# File 'lib/none.rb', line 61

def map(f=nil, &block)
  self
end

#map_none(f = nil) { ... } ⇒ Option<B>

Applies ‘f’ or the block and returns a new ‘Option’ with the returned value.

Parameters:

  • f (Proc<B>) (defaults to: nil)

    The function to call. Could be a block instead. Takes nothing and returns a B.

Yields:

  • Will yield a block that takes nothing and returns a B. Same as ‘f’ parameter.

Returns:

  • (Option<B>)

    A new ‘Option<B>’ whose ‘B’ is the return of ‘f’ or the block.



74
75
76
# File 'lib/none.rb', line 74

def map_none(f=nil, &block)
  Some.new((f || block).call())
end

#none?Boolean

Identifies that this is a ‘None’. For counterparts:

Returns:

  • (Boolean)

    ‘true` because this is a `None’.

See Also:



85
86
87
# File 'lib/none.rb', line 85

def none?()
  true
end

#or_else(f = nil) { ... } ⇒ Some<B> | None

Invokes ‘f’ or the block and returns the Option returned from that. The return type is enforced.

Parameters:

  • f (Proc<Option<B>>) (defaults to: nil)

    The function to call. Could be a block instead. Takes nothing and must return a [Option<B>].

Yields:

  • Will yield a block that takes nothing and returns a Option<B>. Same as ‘f’ parameter.

Returns:

  • (Some<B> | None)

    A new Option from ‘f’ or the block. If ‘f’ returns a non-Option, this will raise ‘OptionReturnExpectedError’.



99
100
101
102
103
104
105
106
# File 'lib/none.rb', line 99

def or_else(f=nil, &block)
  option = (f || block).call()
  if !option.kind_of?(Option)
    raise OptionReturnExpectedError.new(option)
  else
    option
  end
end

#some?Boolean

Identifies that this is not a ‘Some’. For counterparts:

Returns:

  • (Boolean)

    ‘false` because this is not a `Some’.

See Also:



115
116
117
# File 'lib/none.rb', line 115

def some?()
  false
end

#unwrapA

Dangerously try to access the ‘Option’ data. If this is a ‘None’, an exception will be raised. It is recommended to use this for tests only.

Returns:

  • (A)

    The inner data of this ‘Option’.

Raises:



123
124
125
126
127
# File 'lib/none.rb', line 123

def unwrap()
  raise UnwrapError.new(
    "#{self.class} could not be unwrapped as a Some.",
  )
end

#unwrap_nonenil

Dangerously access the ‘None’ data. If this is a ‘Some’, an exception will be raised. It is recommended to use this for tests only.

Returns:

  • (nil)

    Returns nil for ‘None’.



133
134
135
# File 'lib/none.rb', line 133

def unwrap_none()
  nil
end