Class: String
Instance Method Summary collapse
-
#to_boolean ⇒ Object
Attempts to cast the string to
trueorfalse.
Instance Method Details
#to_boolean ⇒ Object
Attempts to cast the string to true or false. This is done
by comparison with known truthy and falsey values.
"Yes".to_boolean #=> true
"1".to_boolean #=> true
"NO".to_boolean #=> false
"FaLsE".to_boolean #=> false
15 16 17 18 19 20 |
# File 'lib/tfg/support/core_ext/string/to_boolean.rb', line 15 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 |