Sha256: fa114a5fd86708bade1f1b39fc6ca10b99ecf0d9e9138c29da3009f1285d7d4f

Contents?: true

Size: 596 Bytes

Versions: 3

Compression:

Stored size: 596 Bytes

Contents

class String
  # Attempts to cast the string to `true` or `false`. This is done
  # by comparison with known truthy and falsey values.
  #
  # ```ruby
  # "Yes".to_boolean #=> true
  # "1".to_boolean #=> true
  #
  # "NO".to_boolean #=> false
  # "FaLsE".to_boolean #=> false
  # ```
  #
  # @raise ArgumentError when the string does not match either a
  #   truthey or falsey value.
  def to_boolean
    return true if self =~ (/\A(true|t|yes|y|1)\Z/i)
    return false if self =~ (/\A(false|f|no|n|0)\Z/i)

    raise ArgumentError.new("String \"#{self}\" cannot be cast to boolean.")
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tfg_support-1.1.1 lib/tfg/support/core_ext/string/to_boolean.rb
tfg_support-1.0.1 lib/tfg/support/core_ext/string/to_boolean.rb
tfg_support-1.0.0 lib/tfg/support/core_ext/string/to_boolean.rb