Module: BooleanFluxCapacitor

Defined in:
lib/web47core/boolean_flux_capacitor.rb

Overview

Convert the thingy listed below to a true/false value

BooleanFluxCapacitor

Provides a common to_bool method for core Ruby types, similar in spirit to to_s or to_i.

The goal is to produce TOTAL boolean coercion:

- Always returns `true` or `false`
- Never raises
- Never returns `nil`

Conversion rules are explicit and deterministic.

Constant Summary collapse

TRUTHY =

Truthy string values (case-insensitive, whitespace ignored)

/\A(true|on|yes|y|1)\z/i

Instance Method Summary collapse

Instance Method Details

#to_boolObject

Convert the receiver into a Boolean value.

This method is intended for configuration, environment variables, and user-supplied input where values may arrive as different types.

Conversion rules:

  • truetrue

  • falsefalse

  • nilfalse

  • Numeric values:

    • 0false

    • non-zero → true

  • String values:

    • Case-insensitive match of:

      "true", "on", "yes", "y", "1" → `true`
      
    • All other strings → false

  • All other object types:

    • Converted using to_s and evaluated using the same string rules

Notes:

  • This method does NOT affect Ruby’s native truthiness.

  • It exists solely for explicit boolean coercion.

Examples:

true.to_bool            # => true
false.to_bool           # => false
nil.to_bool             # => false

0.to_bool               # => false
42.to_bool              # => true

"true".to_bool          # => true
" YES ".to_bool         # => true
"false".to_bool         # => false
"anything else".to_bool # => false


61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/web47core/boolean_flux_capacitor.rb', line 61

def to_bool
  case self
  when true
    true
  when false, nil
    false
  when Numeric
    !zero?
  else
    to_s.strip.match?(TRUTHY)
  end
end