class String


str.terminate([“”, “.”]) # => “Hello World.”
str.compress # => “Hello, World!”
str.clean # => “Hello World”
str = “ Hello, World! ”
@example
compressing, and manipulating strings.
This module extends the String class with additional methods for cleaning,
String extensions for RandomWords

def alignment?

def alignment?
  self =~ /^[\s|:-]+$/ ? true : false
end

def cap_first

Returns:
  • (String) - The string with the first letter capitalized.
def cap_first
  # Capitalizes the first letter of a string.
  self[0] = self[0].upcase
  self
end

def capitalize

Returns:
  • (String) - The string with the first letter capitalized.
def capitalize
  return self if empty?
  letters = split('')
  string = []
  string << letters.shift while letters[0] !~ /[[:word:]]/
  string << letters.shift.upcase
  string.concat(letters)
  string.join('')
end

def capitalize_i

Returns:
  • (String) - The string with "i" capitalized.
def capitalize_i
  gsub(/\bi\b/, 'I')
end

def clean

Returns:
  • (String) - The cleaned string.
  • (String) - The string with unwanted characters removed.
def clean
  gsub(/[^-a-zA-Z0-9\s]/, '').strip
end

def compress

Returns:
  • (String) - The string with extra spaces compressed.
def compress
  gsub(/\s+/, ' ').strip
end

def compress_newlines

Returns:
  • (String) - The string with extra newlines removed.
def compress_newlines
  # Removes extra newlines from the output.
  gsub(/\n{2,}/, "\n\n").strip
end

def compress_newlines!

Returns:
  • (String) - The string with newlines compressed.

Other tags:
    See: #compress_newlines -
def compress_newlines!
  # Removes extra newlines from the output.
  replace compress_newlines
end

def dedup_punctuation

Returns:
  • (String) - The string with duplicate commas removed.
def dedup_punctuation
  gsub(/([,.;:—] ?)+/, '\1').gsub(/([,;:—])/, '\1')
end

def downcase_first

Returns:
  • (String) - The string with the first letter downcased.
def downcase_first
  return self if empty?
  letters = split('')
  string = []
  string << letters.shift while letters[0] !~ /[[:word:]]/
  string << letters.shift.downcase
  string.concat(letters)
  string.join('')
end

def ensure_pipes

#
# @return [String] string with pipes
#
# Ensure leading and trailing pipes
#
def ensure_pipes
  strip.gsub(/^\|?(.*?)\|?$/, '|\1|')
end

def fix_caps(terminators)

Returns:
  • (String) - The string with the first letter of each sentence capitalized.

Parameters:
  • terminators (Array) -- An array of beginning and ending punctuation mark arrays.
def fix_caps(terminators)
  return self if empty?
  terminator_ends = terminators.map { |t| t[1].split('').last }.delete_if(&:empty?)
  return capitalize if terminator_ends.empty?
  terminator_regex = Regexp.new("[#{Regexp.escape(terminator_ends.join)}]")
  return capitalize unless self =~ terminator_regex
  split(/(?<=#{terminator_regex}) /).map do |sentence|
    sentence.capitalize
  end.join(' ')
end

def indent(string)

Returns:
  • (String) - The indented string.

Parameters:
  • string (String) -- The string to indent with.
def indent(string)
  gsub(/^/, string)
end

def no_term(terminators)

Returns:
  • (String) - The string with the last punctuation mark removed.
def no_term(terminators)
  str = dup
  leading = terminators.map { |t| t[0] }.delete_if(&:empty?).sort.uniq.join
  trailing = terminators.map { |t| t[1] }.delete_if(&:empty?).sort.uniq.join
  return self if leading.empty? && trailing.empty?
  str.gsub!(/[#{Regexp.escape(leading)}]+/, '') unless leading.empty?
  str.gsub!(/[#{Regexp.escape(trailing)}]+/, '') unless trailing.empty?
  str
end

def preserve_spaces

Returns:
  • (String) - The string with spaces preserved.
def preserve_spaces
  # Preserves spaces in the output.
  gsub(/ +/, '%%')
end

def restore_spaces

Returns:
  • (String) - The string with spaces restored.
def restore_spaces
  # Restores spaces in the output.
  gsub('%%', ' ')
end

def split_lines

Returns:
  • (Array) - The string split into lines, cleaned, and empty lines removed.
def split_lines
  strip.split("\n").map(&:clean).reject(&:empty?)
end

def term(char = '.')

Returns:
  • (String) - The string with the specified character at the end.

Parameters:
  • char (String) -- The character to add (default: '.').
def term(char = '.')
  # Adds a specified character to the end of a string.
  sub(/[.?!,;]?$/, "#{char}")
end

def terminate(terminator)

Returns:
  • (String) - The string with a random punctuation mark at the end.

Parameters:
  • terminator (Array) -- An array of beginning and ending punctuation marks.
def terminate(terminator)
  sub(/^/, terminator[0]).sub(/[^a-zA-Z0-9]*$/, terminator[1])
end

def to_length

Returns:
  • (Symbol) - The symbolized length of the string.
def to_length
  case self
  when /^s/i
    :short
  when /^m/i
    :medium
  when /^l/i
    :long
  when /^v/i
    :very_long
  else
    :medium
  end
end

def to_sent(terminator)

Returns:
  • (String) - The string with a random punctuation mark at the end.

Parameters:
  • terminator (Array) -- An array of beginning and ending punctuation marks.
def to_sent(terminator)
  capitalize.compress.capitalize_i.dedup_punctuation.terminate(terminator)
end

def to_source

Returns:
  • (Symbol) - The symbolized name of the source.
def to_source
  new_source = nil
  sources = RandomWords::Generator.new.sources
  sources.each do |_k, v|
    v.names.each do |name|
      next unless name.to_s =~ /^#{self}/i
      new_source = v.name
      break
    end
    break if new_source
  end
  new_source || :latin
end

def trueish?

Returns:
  • (Boolean) - true if the string is trueish, false otherwise.
def trueish?
  to_s =~ /^[ty1]/i
end