Sha256: 126697390cd1e5a0f82ec7584f1e94f56adbdb9688c42b0745618f30e1732c1d

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

module Slothify

  class InvalidLengthError < StandardError; end
  DEFAULT_LENGTH = 3
  MIN_LENGTH = 1
  MAX_LENGTH = 300 # Because really, you're just taking the piss now

  refine String do
    # Slothify your strings
    #
    # Example:
    #   >> "Hello, world!".slothify(5)
    #   => "Helloooooo, worldddddd!"
    #
    # Arguments:
    #   length: (Fixnum)
    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

    private

    def valid_length(l)
      (MIN_LENGTH..MAX_LENGTH) === l
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slothify-1.0.0 lib/slothify/string.rb