Truthyable
trait Truthyable
Truthyable: what counts as true when a value is used as a condition.
The rule is Crystal's, and it is short: only false, None and () are falsy. Everything else is truthy, including 0, "" and [], which some languages treat as false and Kex deliberately does not.
0.truthy? # => true
"".truthy? # => true
[].truthy? # => true
None.truthy? # => false
false.truthy? # => false
When you want the "is there anything here" question instead, that is Blankable's blank? / present?.
There is no NaN to consider: a float operation that would produce one raises instead, matching BEAM (see nonFiniteFloatError in src/interpreter/value.cxx).
Implemented by Bool, Integer, Float, String, Optional<X>, [X], Map<K, V>.
Required methods
An implementor writes these.
type Bool
Implements Blankable, Truthyable.
truthy? Truthyable
truthy? : Bool
Returns the boolean itself.
Returns the value
Examples
true.truthy? # => true
false.truthy? # => false
not
not : Bool
Returns the negation of this boolean.
!flag says the same thing, and is the spelling to reach for when the value is already to hand. This one exists for the position ! cannot take: the end of a chain, where what is being negated is whatever the chain just produced. falsy? answers the same question for any Truthyable value; not is the one that both takes and answers a Bool.
Returns false for true, and true for false
Examples
true.not # => false
false.not # => true
At the end of a chain
book.borrowed?.not
Defined in other modules
extends Integer
More methods of Integer, added by this module.
Implements Truthyable.
truthy? Truthyable
truthy? : Bool
Always true, including for zero.
Zero is a number, not an absence. Compare it explicitly when zero means something: count == 0.
Returns always true
Examples
0.truthy? # => true
42.truthy? # => true
extends Float
More methods of Float, added by this module.
Implements Truthyable.
truthy? Truthyable
truthy? : Bool
Always true, including for zero.
Returns always true
Examples
0.0.truthy? # => true
extends String
More methods of String, added by this module.
Implements Truthyable.
truthy? Truthyable
truthy? : Bool
Always true, including for the empty string.
Use blank? from Blankable when an empty or whitespace-only string should count as nothing.
Returns always true
Examples
"".truthy? # => true
"".blank? # => true (the question usually meant)
extends Optional<X>
More methods of Optional, added by this module.
Implements Truthyable.
truthy? Truthyable
truthy? : Bool
Returns true for a Just and false for None.
The one type where truthiness is genuinely about presence, which is what makes an optional usable directly as a condition.
Returns true when a value is present
Examples
Just(0).truthy? # => true
None.truthy? # => false
extends [X]
More methods of List, added by this module.
Implements Truthyable.
truthy? Truthyable
truthy? : Bool
Always true, including for the empty list.
Use empty? or blank? when an empty list should count as nothing.
Returns always true
Examples
[].truthy? # => true
[].blank? # => true (the question usually meant)
extends Map<K, V>
More methods of Map, added by this module.
Implements Truthyable.
truthy? Truthyable
truthy? : Bool
Always true, including for the empty map.
Returns always true
Examples
{}.truthy? # => true
{}.blank? # => true (the question usually meant)