Class: Philiprehberger::Maybe::None
- Inherits:
-
Object
- Object
- Philiprehberger::Maybe::None
- Includes:
- Enumerable, Singleton
- Defined in:
- lib/philiprehberger/maybe.rb
Overview
Represents an absent value
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Equality check.
-
#[](_key) ⇒ None
Access a single key (shorthand for dig).
-
#deconstruct_keys(_keys) ⇒ Hash
Pattern matching support.
-
#dig ⇒ None
Dig always returns None.
-
#each(&block) ⇒ Enumerator, self
Yield nothing for Enumerable support.
-
#filter ⇒ None
No-op filter.
-
#flat_map ⇒ None
No-op transformation.
-
#inspect ⇒ String
(also: #to_s)
String representation.
-
#map ⇒ None
No-op transformation.
-
#none? ⇒ Boolean
True.
-
#or_else(default = nil) { ... } ⇒ Some, None
Return the default value.
-
#or_raise(error_class = Error, message = 'value is absent') ⇒ Object
Raise an error since value is absent.
-
#recover { ... } ⇒ Some, None
Convert None to Some via a block.
-
#some? ⇒ Boolean
False.
-
#tap ⇒ None
No-op tap, returns self.
-
#value ⇒ nil
Always nil.
-
#zip ⇒ None
Combine with other Maybes — always returns None.
Instance Method Details
#==(other) ⇒ Boolean
Returns equality check.
301 302 303 |
# File 'lib/philiprehberger/maybe.rb', line 301 def ==(other) other.is_a?(None) end |
#[](_key) ⇒ None
Access a single key (shorthand for dig)
266 267 268 |
# File 'lib/philiprehberger/maybe.rb', line 266 def [](_key) self end |
#deconstruct_keys(_keys) ⇒ Hash
Pattern matching support
274 275 276 |
# File 'lib/philiprehberger/maybe.rb', line 274 def deconstruct_keys(_keys) { value: nil, some: false, none: true } end |
#dig ⇒ None
Dig always returns None
258 259 260 |
# File 'lib/philiprehberger/maybe.rb', line 258 def dig(*) self end |
#each(&block) ⇒ Enumerator, self
Yield nothing for Enumerable support
194 195 196 197 198 |
# File 'lib/philiprehberger/maybe.rb', line 194 def each(&block) return to_enum(:each) unless block self end |
#filter ⇒ None
No-op filter
232 233 234 |
# File 'lib/philiprehberger/maybe.rb', line 232 def filter self end |
#flat_map ⇒ None
No-op transformation
225 226 227 |
# File 'lib/philiprehberger/maybe.rb', line 225 def flat_map self end |
#inspect ⇒ String Also known as: to_s
Returns string representation.
306 307 308 |
# File 'lib/philiprehberger/maybe.rb', line 306 def inspect 'None' end |
#map ⇒ None
No-op transformation
218 219 220 |
# File 'lib/philiprehberger/maybe.rb', line 218 def map self end |
#none? ⇒ Boolean
Returns true.
211 212 213 |
# File 'lib/philiprehberger/maybe.rb', line 211 def none? true end |
#or_else(default = nil) { ... } ⇒ Some, None
Return the default value
241 242 243 244 |
# File 'lib/philiprehberger/maybe.rb', line 241 def or_else(default = nil, &block) value = block ? block.call : default Maybe.wrap(value) end |
#or_raise(error_class = Error, message = 'value is absent') ⇒ Object
Raise an error since value is absent
251 252 253 |
# File 'lib/philiprehberger/maybe.rb', line 251 def or_raise(error_class = Error, = 'value is absent') raise error_class, end |
#recover { ... } ⇒ Some, None
Convert None to Some via a block
282 283 284 |
# File 'lib/philiprehberger/maybe.rb', line 282 def recover(&block) Maybe.wrap(block.call) end |
#some? ⇒ Boolean
Returns false.
206 207 208 |
# File 'lib/philiprehberger/maybe.rb', line 206 def some? false end |
#tap ⇒ None
No-op tap, returns self
296 297 298 |
# File 'lib/philiprehberger/maybe.rb', line 296 def tap self end |
#value ⇒ nil
Returns always nil.
201 202 203 |
# File 'lib/philiprehberger/maybe.rb', line 201 def value nil end |
#zip ⇒ None
Combine with other Maybes — always returns None
289 290 291 |
# File 'lib/philiprehberger/maybe.rb', line 289 def zip(*) self end |