Module: BooleanCasting

Included in:
Kernel
Defined in:
lib/core/utils/extensions/string/to_bool.rb

Overview

Adds some useful methods to the String class

Instance Method Summary collapse

Instance Method Details

#to_boolBoolean Also known as: to_b, to_boolean

Casts a string to a boolean

Returns:

  • (Boolean)

    the boolean value of the string



5
6
7
8
9
10
11
12
13
# File 'lib/core/utils/extensions/string/to_bool.rb', line 5

def to_bool
  return self if [true, false].include?(self)
  return false if nil? || empty?
  return false if self =~ /^(false|f|no|n|0)$/i
  return true if self =~ /^(true|t|yes|y|1)$/i

  # If the string is not a boolean, return false by default
  false
end