Class: MonadOxide::Some
Overview
‘Some’ represents a present value in an ‘Option’. For most operations, ‘Some’ will perform some operation.
Instance Method Summary collapse
-
#and_then(f = nil) { ... } ⇒ Some<B> | None
Invokes ‘f’ or the block with the data and returns the Option returned from that.
-
#initialize(data) ⇒ Some
constructor
Constructs a ‘Some’ with the data provided.
-
#inspect_none(f = nil) { ... } ⇒ Some
Falls through.
-
#inspect_some(f = nil) { ... } ⇒ Option<A>
Applies ‘f’ or the block over the data and returns the same ‘Some’.
-
#map(f = nil) { ... } ⇒ Option<B>
Applies ‘f’ or the block over the data and returns a new ‘Some’ with the returned value.
-
#map_none(f = nil) { ... } ⇒ Option<A>
This is a no-op for Some.
-
#none? ⇒ Boolean
Identifies that this is not a ‘None’.
-
#or_else(f = nil) { ... } ⇒ Option<A>
The None equivalent to Some#and_then.
-
#some? ⇒ Boolean
Identifies that this is a ‘Some’.
-
#unwrap ⇒ A
Dangerously access the ‘Some’ data.
-
#unwrap_none ⇒ nil
Dangerously access the ‘None’ data.
Methods inherited from Option
Constructor Details
#initialize(data) ⇒ Some
Constructs a ‘Some’ with the data provided.
16 17 18 |
# File 'lib/some.rb', line 16 def initialize(data) @data = data end |
Instance Method Details
#and_then(f = nil) { ... } ⇒ Some<B> | None
Invokes ‘f’ or the block with the data and returns the Option returned from that. The return type is enforced.
30 31 32 33 34 35 36 37 |
# File 'lib/some.rb', line 30 def and_then(f=nil, &block) option = (f || block).call(@data) if !option.kind_of?(Option) raise OptionReturnExpectedError.new(option) else option end end |
#inspect_none(f = nil) { ... } ⇒ Some
Falls through. @see Option#inspect_none for how this is handled in either Option case, and @see None#inspect_none for how this is handled in the None case.
59 60 61 |
# File 'lib/some.rb', line 59 def inspect_none(f=nil, &block) self end |
#inspect_some(f = nil) { ... } ⇒ Option<A>
Applies ‘f’ or the block over the data and returns the same ‘Some’. No changes are applied. This is ideal for logging.
47 48 49 50 |
# File 'lib/some.rb', line 47 def inspect_some(f=nil, &block) (f || block).call(@data) self end |
#map(f = nil) { ... } ⇒ Option<B>
Applies ‘f’ or the block over the data and returns a new ‘Some’ with the returned value.
72 73 74 |
# File 'lib/some.rb', line 72 def map(f=nil, &block) self.class.new((f || block).call(@data)) end |
#map_none(f = nil) { ... } ⇒ Option<A>
This is a no-op for Some. @see None#map_none.
81 82 83 |
# File 'lib/some.rb', line 81 def map_none(f=nil, &block) self end |
#none? ⇒ Boolean
Identifies that this is not a ‘None’. For counterparts:
92 93 94 |
# File 'lib/some.rb', line 92 def none?() false end |
#or_else(f = nil) { ... } ⇒ Option<A>
The None equivalent to Some#and_then. This is a no-op for Some. @see None#or_else.
102 103 104 |
# File 'lib/some.rb', line 102 def or_else(f=nil, &block) self end |
#some? ⇒ Boolean
Identifies that this is a ‘Some’. For counterparts:
113 114 115 |
# File 'lib/some.rb', line 113 def some?() true end |
#unwrap ⇒ A
Dangerously access the ‘Some’ data. If this is a ‘None’, an exception will be raised. It is recommended to use this for tests only.
121 122 123 |
# File 'lib/some.rb', line 121 def unwrap() @data end |
#unwrap_none ⇒ nil
Dangerously access the ‘None’ data. If this is a ‘Some’, an exception will be raised. It is recommended to use this for tests only.
129 130 131 132 133 134 135 136 |
# File 'lib/some.rb', line 129 def unwrap_none() raise UnwrapError.new( " \#{self.class} with \#{@data.inspect()} could not be unwrapped as a\n None.\n EOE\n )\nend\n" |