Class: String
- Defined in:
- lib/a_types/core_ext/boolean.rb,
lib/a_types/core_ext/numeric.rb
Overview
Numeric Additions.
Instance Method Summary collapse
-
#to_bool ⇒ true, false
(also: #to_b)
Converts this String to a boolean value.
-
#to_bool! ⇒ true, false
(also: #to_b!)
Converts this String to a boolean value.
-
#to_num ⇒ Numeric?
Will convert any string with a valid number into the correct type (Fixnum or Float).
Instance Method Details
#to_bool ⇒ true, false Also known as: to_b
Converts this String to a boolean value.
-
Single characters are converted to true if they are part of the set
['y', 'Y', '1']
-
Multiple characters are converted to true if they are part of the set
['yes', 'Yes', 'YES', 'true', 'True', 'TRUE']
-
In any other case, the result will be false.
33 34 35 |
# File 'lib/a_types/core_ext/boolean.rb', line 33 def to_bool %w(y Y 1 yes Yes YES true True TRUE).include?(self) end |
#to_bool! ⇒ true, false Also known as: to_b!
Converts this String to a boolean value.
-
Single characters are converted to true if they are part of the set
['y', 'Y', '1']
-
Single characters are converted to false if they are part of the set
['n', 'N', '0', '']
-
Multiple characters are converted to true if they are part of the set
['yes', 'Yes', 'YES', 'true', 'True', 'TRUE']
-
Multiple characters are converted to false if they are part of the set
['no', 'No', 'NO', 'false', 'False', 'FALSE']
-
In any other case, an ArgumentError will be raised.
56 57 58 59 60 61 62 63 |
# File 'lib/a_types/core_ext/boolean.rb', line 56 def to_bool! return true if %w(y Y 1 yes Yes YES true True TRUE).include?(self) negative = ['n', 'N', '0', 'no', 'No', 'NO', 'false', 'False', 'FALSE', ''] return false if negative.include?(self) fail ArgumentError, "#{self} can't be interpreted as boolean!" end |
#to_num ⇒ Numeric?
Will convert any string with a valid number into the correct type (Fixnum or Float). If this String does not contain a valid number, nil will be returned.
31 32 33 34 35 36 37 |
# File 'lib/a_types/core_ext/numeric.rb', line 31 def to_num if self =~ /^-?\d+$/ to_i elsif self =~ /^-?\d+[,.]\d+$/ sub(',', '.').to_f end end |