Class: String

Inherits:
Object show all
Defined in:
lib/srs_game.rb

Constant Summary

TRUE_WORDS =
%w{t true yes y}
FALSE_WORDS =
%w{f false no n}

Instance Method Summary (collapse)

Instance Method Details

- (Array) args

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]

Returns:

  • (Array)


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?

Returns:

  • (Boolean)


85
86
87
# File 'lib/srs_game.rb', line 85

def boolean?
  !to_bool.nil?
end

- (Boolean) numeric?

Does the string represent a numeral in Ruby?

Returns:

  • (Boolean)


91
92
93
# File 'lib/srs_game.rb', line 91

def numeric?
  !!Float(self) rescue false
end

- (Boolean?) to_bool

Convert to boolean value matching TRUE_WORDS or FALSE_WORDS

Returns:

  • (Boolean, nil)


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

- (Array) words

Scans the string for groups of non-whitespace characters.

Returns:

  • (Array)


115
116
117
# File 'lib/srs_game.rb', line 115

def words
  scan(/\S+/)
end