class RandomWords::Generator
def characters(min, max = nil, whole_words: true, whitespace: true, dead_switch: 0)
-
(String)
- The generated string of random words
Parameters:
-
dead_switch
(Integer
) -- (Optional) A counter to prevent infinite loops -
whole_words
(Boolean
) -- (Optional) Whether to generate whole words or not -
max
(Integer
) -- (Optional) The maximum length of the generated string -
min
(Integer
) -- The minimum length of the generated string
def characters(min, max = nil, whole_words: true, whitespace: true, dead_switch: 0) result = '' max ||= min raise ArgumentError, 'Infinite loop detected' if dead_switch > 20 whole_words = false if dead_switch > 15 space = whitespace ? ' ' : '' current_part = 0 while result.length < max && result.length < min word = send(SENTENCE_PARTS[current_part].to_sym) word.gsub!(/ +/, '') unless whitespace current_part = (current_part + 1) % SENTENCE_PARTS.length new_result = "#{result}#{space}#{word}".compress if new_result.length > max return handle_overflow(OverflowConfig.new(new_result, result, min, max, whole_words, whitespace, dead_switch)) end return new_result if new_result.length == max result = new_result end result.strip end