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 article_agree
def article_agree gsub(/\ban? (\w)/i) do |match| word = match[1] /\A[aeiou]/i.match?(word) ? "an #{word}" : "a #{word}" end end
def cap_first
-
(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
-
(String)- The string with the first letter capitalized.
def capitalize return self if empty? str = dup debug = '' str.sub!(/^%[A-Z]+%/) do |match| debug = match '' end letters = str.split('') string = [] string << letters.shift while letters[0] !~ /[[:word:]]/ string << letters.shift.upcase string.concat(letters) debug + string.join('') end
def capitalize_i
-
(String)- The string with "i" capitalized.
def capitalize_i gsub(/\bi\b/, 'I') end
def clean
-
(String)- The cleaned string. -
(String)- The string with unwanted characters removed.
def clean gsub(/[^-a-zA-Z0-9\s]/, '').strip end
def clean_encode
# @return [String] UTF-8 string
#
# Get a clean UTF-8 string by forcing an ISO encoding and then re-encoding
#
def clean_encode force_encoding('ISO-8859-1').encode('utf-8', replace: nil) end
def clean_encode!
# @return [String] UTF-8 string, in place
#
# Destructive version of #clean_encode
#
def clean_encode! replace clean_encode end
def clean_output
def clean_output encode('iso-8859-1', undef: :replace, invalid: :replace, replace: '').force_encoding('UTF-8') end
def colorize_text(text, color, testing: false)
-
(String)- The colorized text.
Parameters:
-
color(Symbol) -- The color to use (e.g., :red, :green). -
text(String) -- The text to colorize.
def colorize_text(text, color, testing: false) return text if !$stdout.isatty && !testing return text unless colors.key?(color) return text if text.empty? color_code = colors[color] "\e[#{color_code}m#{text}\e[0m" end
def colors
def colors { black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, cyan: 36, white: 37, boldblack: '1;30', boldred: '1;31', boldgreen: '1;32', boldyellow: '1;33', boldblue: '1;34', boldmagenta: '1;35', boldcyan: '1;36', boldwhite: '1;37', reset: 0 } end
def compress
-
(String)- The string with extra spaces compressed.
def compress gsub(/\s+/, ' ').strip end
def compress_newlines
-
(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!
-
(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
-
(String)- The string with duplicate punctuation removed.
def dedup_punctuation gsub(/((%[A-Z]+%)?[,.;:—] ?)+/, '\1').gsub(/([,;:—])/, '\1') end
def downcase_first
-
(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 expand_debug(testing: false)
-
(String)- The expanded debug string.
def expand_debug(testing: false) gsub(/%(#{Regexp.union(expansions.keys)})%?/) do match = Regexp.last_match return match unless expansions.key?(match[1]) colorize_text("[#{expansions[match[1]][0] || match}]", expansions[match[1]][1] || :white, testing: testing) end end
def expansions
def expansions { 'ADJ' => ['Adjective', :yellow], 'ADV' => ['Adverb', :boldyellow], 'ART' => ['Article', :cyan], 'CLA' => ['Clause', :magenta], 'COC' => ['Coordinating Conjunction', :magenta], 'CON' => ['Conjunction', :magenta], 'NAM' => ['Name', :boldblue], 'NOU' => ['Noun', :green], 'NUM' => ['Number', :red], 'PAV' => ['Passive Verb', :boldred], 'PHR' => ['Phrase', :boldcyan], 'PLA' => ['Plural Article', :yellow], 'PLN' => ['Plural Noun', :green], 'PLV' => ['Plural Verb', :boldred], 'PNO' => ['Proper Noun', :boldblack], 'PRE' => ['Preposition', :boldwhite], 'PRP' => ['Prepositional Phrase', :boldwhite], 'SEP' => ['Separator', :red], 'SUC' => ['Subordinate Conjunction', :magenta], 'TER' => ['Terminator', :boldcyan], 'VER' => ['Verb', :boldred] } end
def fix_caps(terminators)
-
(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 match?(terminator_regex) split(/(?<=#{terminator_regex}) /).map do |sentence| sentence.capitalize end.join(' ') end
def indent(string)
-
(String)- The indented string.
Parameters:
-
string(String) -- The string to indent with.
def indent(string) gsub(/^/, string) end
def no_term(terminators)
-
(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
-
(String)- The string with spaces preserved.
def preserve_spaces # Preserves spaces in the output. gsub(/ +/, '%%') end
def restore_spaces
-
(String)- The string with spaces restored.
def restore_spaces # Restores spaces in the output. gsub('%%', ' ') end
def split_lines
-
(Array- The string split into lines, cleaned, and empty lines removed.)
def split_lines arr = strip.split("\n").map(&:clean) arr.reject!(&:empty?) arr end
def term(char = '.')
-
(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)
-
(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) debug = '' str = dup str.sub!(/^%[A-Z]+%/) do |match| debug = match '' end debug + str.sub(/^/, terminator[0]).sub(/[^a-zA-Z0-9]*$/, terminator[1]) end
def to_length
-
(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)
-
(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
-
(Symbol)- The symbolized name of the source.
def to_source new_source = nil sources = RandomWords::Generator.new.sources return sources.map { |k, v| v.name }.sample if /^(random|any)$/i.match?(self) sources.each do |_k, v| v.names.each do |name| next unless /^#{self}/i.match?(name.to_s) new_source = v.name break end break if new_source end new_source || :latin end
def trueish?
-
(Boolean)- true if the string is trueish, false otherwise.
def trueish? to_s =~ /^[ty1]/i end