kex docs Standard Library

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 methods

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 methods of Bool, added by this module.

Implements Blankable.

blank? Blankable

blank? : Bool

Returns true for false: the only blank boolean.

Returns true when the value is false

Examples
false.blank?   # => true
true.blank?    # => false

extends Integer

More methods of Integer, added by this module.

Implements Blankable.

blank? Blankable

blank? : Bool

Always false: every integer is a value, including zero.

Returns always false

Examples
0.blank?    # => false
42.blank?   # => false

extends Float

More methods of Float, added by this module.

Implements Blankable.

blank? Blankable

blank? : Bool

Always false: every float is a value, including zero.

Returns always false

Examples
0.0.blank?   # => false

extends String

More methods of 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 methods of 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 methods of List, added by this module.

Implements Blankable.

blank? Blankable

blank? : Bool

Returns true when the list has no elements.

Returns true for the empty list

Examples
[].blank?         # => true
[1, 2].blank?     # => false

Reporting an empty result set

if matches.blank?
  IO.printLine("no matches")
end