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.
438 439 440 |
# File 'lib/philiprehberger/maybe.rb', line 438 def ==(other) other.is_a?(None) end |
#[](_key) ⇒ None
Access a single key (shorthand for dig)
362 363 364 |
# File 'lib/philiprehberger/maybe.rb', line 362 def [](_key) self end |
#contains?(_value) ⇒ Boolean
None never contains anything
414 415 416 |
# File 'lib/philiprehberger/maybe.rb', line 414 def contains?(_value) false end |
#deconstruct_keys(_keys) ⇒ Hash
Pattern matching support
370 371 372 |
# File 'lib/philiprehberger/maybe.rb', line 370 def deconstruct_keys(_keys) { value: nil, some: false, none: true } end |
#dig ⇒ None
Dig always returns None
354 355 356 |
# File 'lib/philiprehberger/maybe.rb', line 354 def dig(*) self end |
#each(&block) ⇒ Enumerator, self
Yield nothing for Enumerable support
290 291 292 293 294 |
# File 'lib/philiprehberger/maybe.rb', line 290 def each(&block) return to_enum(:each) unless block self end |
#filter ⇒ None
No-op filter
328 329 330 |
# File 'lib/philiprehberger/maybe.rb', line 328 def filter self end |
#flat_map ⇒ None
No-op transformation
321 322 323 |
# File 'lib/philiprehberger/maybe.rb', line 321 def flat_map self end |
#flatten ⇒ None
Flatten a None — returns self
406 407 408 |
# File 'lib/philiprehberger/maybe.rb', line 406 def flatten self end |
#fold(some:, none:) ⇒ Object
Explicit case handling: invokes the ‘none:` proc with no arguments.
431 432 433 434 435 |
# File 'lib/philiprehberger/maybe.rb', line 431 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.
443 444 445 |
# File 'lib/philiprehberger/maybe.rb', line 443 def inspect 'None' end |
#map ⇒ None
No-op transformation
314 315 316 |
# File 'lib/philiprehberger/maybe.rb', line 314 def map self end |
#none? ⇒ Boolean
Returns true.
307 308 309 |
# File 'lib/philiprehberger/maybe.rb', line 307 def none? true end |
#or_else(default = nil) { ... } ⇒ Some, None
Return the default value
337 338 339 340 |
# File 'lib/philiprehberger/maybe.rb', line 337 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
347 348 349 |
# File 'lib/philiprehberger/maybe.rb', line 347 def or_raise(error_class = Error, = 'value is absent') raise error_class, end |
#present? ⇒ Boolean
Alias for #some? — matches Rails-style naming
421 422 423 |
# File 'lib/philiprehberger/maybe.rb', line 421 def present? false end |
#recover { ... } ⇒ Some, None
Convert None to Some via a block
378 379 380 |
# File 'lib/philiprehberger/maybe.rb', line 378 def recover(&block) Maybe.wrap(block.call) end |
#reject ⇒ None
No-op reject, returns self
399 400 401 |
# File 'lib/philiprehberger/maybe.rb', line 399 def reject self end |
#some? ⇒ Boolean
Returns false.
302 303 304 |
# File 'lib/philiprehberger/maybe.rb', line 302 def some? false end |
#tap ⇒ None
No-op tap, returns self
392 393 394 |
# File 'lib/philiprehberger/maybe.rb', line 392 def tap self end |
#value ⇒ nil
Returns always nil.
297 298 299 |
# File 'lib/philiprehberger/maybe.rb', line 297 def value nil end |
#zip ⇒ None
Combine with other Maybes — always returns None
385 386 387 |
# File 'lib/philiprehberger/maybe.rb', line 385 def zip(*) self end |