Class: String
Constant Summary collapse
- TRUE_WORDS =
%w{t true yes y}
- FALSE_WORDS =
%w{f false no n}
Instance Method Summary collapse
-
#args ⇒ Array
Turn the string in to an array of arguments.
-
#boolean? ⇒ Boolean
Can the string be converted to a boolean value?.
-
#numeric? ⇒ Boolean
Does the string represent a numeral in Ruby?.
-
#to_bool ⇒ Boolean?
Convert to boolean value matching TRUE_WORDS or FALSE_WORDS.
-
#words ⇒ Array
Scans the string for groups of non-whitespace characters.
Instance Method Details
#args ⇒ Array
Turn the string in to an array of arguments. It tries to convert words into booleans and floats. Example:
"3.14 yes gaga false".args #=> [3.14, true, "gaga", false]
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/srs_game.rb', line 99 def args strip.words.map do |arg| next if arg.empty? if arg.boolean? arg.to_bool elsif arg.numeric? Float(arg) else arg end end end |
#boolean? ⇒ Boolean
Can the string be converted to a boolean value?
85 86 87 |
# File 'lib/srs_game.rb', line 85 def boolean? !to_bool.nil? end |
#numeric? ⇒ Boolean
Does the string represent a numeral in Ruby?
91 92 93 |
# File 'lib/srs_game.rb', line 91 def numeric? !!Float(self) rescue false end |
#to_bool ⇒ Boolean?
Convert to boolean value matching TRUE_WORDS or FALSE_WORDS
73 74 75 76 77 78 79 80 81 |
# File 'lib/srs_game.rb', line 73 def to_bool dc = to_s.downcase if TRUE_WORDS.include?(dc) true elsif FALSE_WORDS.include?(dc) false end end |
#words ⇒ Array
Scans the string for groups of non-whitespace characters.
115 116 117 |
# File 'lib/srs_game.rb', line 115 def words scan(/\S+/) end |