module Slothify

Constants

DEFAULT_LENGTH
MAX_LENGTH
MIN_LENGTH
VERSION

Public Instance Methods

slothify(length = DEFAULT_LENGTH) click to toggle source

Slothify your strings

Example:

>> "Hello, world!".slothify(5)
=> "Helloooooo, worldddddd!"

Arguments:

length: (Fixnum)
# File lib/slothify/string.rb, line 17
def slothify(length = DEFAULT_LENGTH)
  raise InvalidLengthError, "Please input a length between #{MIN_LENGTH} and #{MAX_LENGTH}" unless valid_length(length)

  # Split up the string to modify one word at at time
  self.split.map do |word|
    # Find the last alphabetic character to extend
    last_alpha_index = word.rindex(/[[:alpha:]]/)
    if last_alpha_index.nil?
      # No alpha characters found, don't do anything
      word
    else
      # Insert the specified number of repeat chars
      word.insert last_alpha_index, word[last_alpha_index] * length
    end
  end.join(" ")
end
valid_length(l) click to toggle source
# File lib/slothify/string.rb, line 36
def valid_length(l)
  (MIN_LENGTH..MAX_LENGTH) === l
end