Blankable
trait Blankable
Blankable: types that can be asked whether they hold anything meaningful.
A value is blank when it has no meaningful content: None, an empty or all-whitespace string, an empty collection, or false. The semantics are Rails's, and the point is the same: one question that works across types, so a validation does not need a different test per field.
"".blank? # => true
" ".blank? # => true (unlike " ".empty?)
[].blank? # => true
None.blank? # => true
"hi".present? # => true
present? is the negation, and is often what reads better:
fields.all? { |name, value| value.present? }
Implemented by Bool, Integer, Float, String, Optional<X>, [X], Map<K, V>, Queue<A>, Set<A>, UnorderedSet<A>, Stack<A>.
Required functions
An implementor writes these.
blank?
blank? : Bool
Returns true when the value holds nothing meaningful.
What that means is up to each type: whitespace-only for a string, no elements for a collection, None for an optional, false for a boolean.
Returns true when the value is blank
Examples
"".blank? # => true
" ".blank? # => true
"hi".blank? # => false
extends Bool
More functions for Bool, added by this module.
Implements Blankable.
extends Integer
More functions for Integer, added by this module.
Implements Blankable.
extends Float
More functions for Float, added by this module.
Implements Blankable.
extends String
More functions for String, added by this module.
Implements Blankable.
blank? Blankable
blank? : Bool
Returns true when the string is empty or contains only whitespace.
This is what separates it from empty?: a string of spaces is not empty, but it is blank, and for user input that is usually the question being asked.
Returns true for an empty or whitespace-only string
Examples
"".blank? # => true
" ".blank? # => true
"\n\t".blank? # => true
" hi ".blank? # => false
Rejecting an empty answer
let name = IO.getLine.or("")
if name.blank?
IO.printError("a name is required")
end
extends Optional<X>
More functions for Optional, added by this module.
Implements Blankable.
blank? Blankable
blank? : Bool
Returns true for None and false for a Just: whatever it wraps.
Note that Just("") is present, not blank: the optional holds a value, even though that value is itself blank.
Returns true for None
Examples
None.blank? # => true
Just(1).blank? # => false
Just("").blank? # => false
extends [X]
More functions for List, added by this module.
Implements Blankable.