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).
-
#contains?(_value) ⇒ Boolean
None never contains anything.
-
#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.
-
#flatten ⇒ None
Flatten a None — returns self.
-
#fold(some:, none:) ⇒ Object
Explicit case handling: invokes the ‘none:` proc with no arguments.
-
#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.
-
#present? ⇒ Boolean
Alias for #some? — matches Rails-style naming.
-
#recover { ... } ⇒ Some, None
Convert None to Some via a block.
-
#reject ⇒ None
No-op reject, returns self.
-
#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.
423 424 425 |
# File 'lib/philiprehberger/maybe.rb', line 423 def ==(other) other.is_a?(None) end |
#[](_key) ⇒ None
Access a single key (shorthand for dig)
347 348 349 |
# File 'lib/philiprehberger/maybe.rb', line 347 def [](_key) self end |
#contains?(_value) ⇒ Boolean
None never contains anything
399 400 401 |
# File 'lib/philiprehberger/maybe.rb', line 399 def contains?(_value) false end |
#deconstruct_keys(_keys) ⇒ Hash
Pattern matching support
355 356 357 |
# File 'lib/philiprehberger/maybe.rb', line 355 def deconstruct_keys(_keys) { value: nil, some: false, none: true } end |
#dig ⇒ None
Dig always returns None
339 340 341 |
# File 'lib/philiprehberger/maybe.rb', line 339 def dig(*) self end |
#each(&block) ⇒ Enumerator, self
Yield nothing for Enumerable support
275 276 277 278 279 |
# File 'lib/philiprehberger/maybe.rb', line 275 def each(&block) return to_enum(:each) unless block self end |
#filter ⇒ None
No-op filter
313 314 315 |
# File 'lib/philiprehberger/maybe.rb', line 313 def filter self end |
#flat_map ⇒ None
No-op transformation
306 307 308 |
# File 'lib/philiprehberger/maybe.rb', line 306 def flat_map self end |
#flatten ⇒ None
Flatten a None — returns self
391 392 393 |
# File 'lib/philiprehberger/maybe.rb', line 391 def flatten self end |
#fold(some:, none:) ⇒ Object
Explicit case handling: invokes the ‘none:` proc with no arguments.
416 417 418 419 420 |
# File 'lib/philiprehberger/maybe.rb', line 416 def fold(some:, none:) raise ArgumentError, 'some: and none: are required' if some.nil? || none.nil? none.call end |
#inspect ⇒ String Also known as: to_s
Returns string representation.
428 429 430 |
# File 'lib/philiprehberger/maybe.rb', line 428 def inspect 'None' end |
#map ⇒ None
No-op transformation
299 300 301 |
# File 'lib/philiprehberger/maybe.rb', line 299 def map self end |
#none? ⇒ Boolean
Returns true.
292 293 294 |
# File 'lib/philiprehberger/maybe.rb', line 292 def none? true end |
#or_else(default = nil) { ... } ⇒ Some, None
Return the default value
322 323 324 325 |
# File 'lib/philiprehberger/maybe.rb', line 322 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
332 333 334 |
# File 'lib/philiprehberger/maybe.rb', line 332 def or_raise(error_class = Error, = 'value is absent') raise error_class, end |
#present? ⇒ Boolean
Alias for #some? — matches Rails-style naming
406 407 408 |
# File 'lib/philiprehberger/maybe.rb', line 406 def present? false end |
#recover { ... } ⇒ Some, None
Convert None to Some via a block
363 364 365 |
# File 'lib/philiprehberger/maybe.rb', line 363 def recover(&block) Maybe.wrap(block.call) end |
#reject ⇒ None
No-op reject, returns self
384 385 386 |
# File 'lib/philiprehberger/maybe.rb', line 384 def reject self end |
#some? ⇒ Boolean
Returns false.
287 288 289 |
# File 'lib/philiprehberger/maybe.rb', line 287 def some? false end |
#tap ⇒ None
No-op tap, returns self
377 378 379 |
# File 'lib/philiprehberger/maybe.rb', line 377 def tap self end |
#value ⇒ nil
Returns always nil.
282 283 284 |
# File 'lib/philiprehberger/maybe.rb', line 282 def value nil end |
#zip ⇒ None
Combine with other Maybes — always returns None
370 371 372 |
# File 'lib/philiprehberger/maybe.rb', line 370 def zip(*) self end |