kex docs Standard Library

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.

truthy?

truthy? : Bool

Returns true when the value counts as true in a condition.

Returns true unless the value is false, None or ()

Examples
1.truthy?        # => true
None.truthy?     # => false

extends Bool

More methods of Bool, added by this module.

Implements Truthyable.

truthy? Truthyable

truthy? : Bool

Returns the boolean itself.

Returns the value

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

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)