Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/random-words/string.rb,
lib/random-words/table-cleanup.rb
Overview
String helpers
Instance Method Summary collapse
- #alignment? ⇒ Boolean
- #article_agree ⇒ Object
-
#cap_first ⇒ String
Capitalize the first letter of a string.
-
#capitalize ⇒ String
Capitalize the first letter of a string, respcting punctuation.
-
#capitalize_i ⇒ String
Capitalize instances of “i” in a string.
-
#clean ⇒ String
Remove unwanted characters and whitespace from a string.
-
#clean_encode ⇒ String
Get a clean UTF-8 string by forcing an ISO encoding and then re-encoding.
-
#clean_encode! ⇒ String
Destructive version of #clean_encode.
- #clean_output ⇒ Object
-
#colorize_text(text, color, testing: false) ⇒ String
Colorize the text for terminal output.
-
#colors ⇒ Object
Terminal output colors.
-
#compress ⇒ String
Compress multiple spaces into a single space and remove leading/trailing spaces.
-
#compress_newlines ⇒ String
Remove extra newlines from the output.
-
#compress_newlines! ⇒ String
Compress newlines in the output, destructive.
-
#dedup_punctuation ⇒ String
Remove duplicate punctuation.
-
#downcase_first ⇒ String
Downcase the first letter of a string, respecting punctuation.
-
#ensure_pipes ⇒ String
Ensure leading and trailing pipes.
-
#expand_debug(testing: false) ⇒ String
Expand abbreviated debug statements in the string.
-
#expansions ⇒ Object
Dictionary of expansions.
-
#fix_caps(terminators) ⇒ String
Capitalize the first letter of each sentence in a string.
-
#indent(string) ⇒ String
Indent every line in a string with a specified string.
-
#no_term(terminators) ⇒ String
Remove any punctuation mark from the end of a string.
-
#preserve_spaces ⇒ String
Preserve spaces in the output by replacing multiple spaces with %%.
-
#restore_spaces ⇒ String
Restore spaces in the output by replacing %% with spaces.
-
#split_lines ⇒ Array<String>
Split a string by newlines, clean each line, and remove empty lines.
-
#term(char = '.') ⇒ String
Add a specified character to the end of a string, avoiding repeated punctuation.
-
#terminate(terminator) ⇒ String
Terminate a string with a random punctuation mark.
-
#to_length ⇒ Symbol
Convert the string to a length symbol based on its name.
-
#to_sent(terminator) ⇒ String
Generate a sentence with capitalization and terminator.
-
#to_source ⇒ Symbol
Convert the string to a source symbol based on its name.
-
#trueish? ⇒ Boolean
Check if the string is trueish (starts with ‘t’, ‘y’, or ‘1’).
Instance Method Details
#alignment? ⇒ Boolean
148 149 150 |
# File 'lib/random-words/table-cleanup.rb', line 148 def alignment? self =~ /^[\s|:-]+$/ ? true : false end |
#article_agree ⇒ Object
354 355 356 357 358 359 |
# File 'lib/random-words/string.rb', line 354 def article_agree gsub(/\ban? (\w)/i) do |match| word = match[1] /\A[aeiou]/i.match?(word) ? "an #{word}" : "a #{word}" end end |
#cap_first ⇒ String
Capitalize the first letter of a string.
184 185 186 187 188 |
# File 'lib/random-words/string.rb', line 184 def cap_first # Capitalizes the first letter of a string. self[0] = self[0].upcase self end |
#capitalize ⇒ String
Capitalize the first letter of a string, respcting punctuation.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/random-words/string.rb', line 59 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 |
#capitalize_i ⇒ String
Capitalize instances of “i” in a string.
51 52 53 |
# File 'lib/random-words/string.rb', line 51 def capitalize_i gsub(/\bi\b/, 'I') end |
#clean ⇒ String
Remove unwanted characters and whitespace from a string.
40 41 42 |
# File 'lib/random-words/string.rb', line 40 def clean gsub(/[^-a-zA-Z0-9\s]/, '').strip end |
#clean_encode ⇒ String
Get a clean UTF-8 string by forcing an ISO encoding and then re-encoding
21 22 23 |
# File 'lib/random-words/string.rb', line 21 def clean_encode force_encoding('ISO-8859-1').encode('utf-8', replace: nil) end |
#clean_encode! ⇒ String
Destructive version of #clean_encode
30 31 32 |
# File 'lib/random-words/string.rb', line 30 def clean_encode! replace clean_encode end |
#clean_output ⇒ Object
361 362 363 |
# File 'lib/random-words/string.rb', line 361 def clean_output encode('iso-8859-1', undef: :replace, invalid: :replace, replace: '').force_encoding('UTF-8') end |
#colorize_text(text, color, testing: false) ⇒ String
Colorize the text for terminal output. If the output is not a TTY, the text is returned without colorization.
303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/random-words/string.rb', line 303 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 |
#colors ⇒ Object
Terminal output colors
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/random-words/string.rb', line 272 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 |
#compress ⇒ String
Compress multiple spaces into a single space and remove leading/trailing spaces.
140 141 142 |
# File 'lib/random-words/string.rb', line 140 def compress gsub(/\s+/, ' ').strip end |
#compress_newlines ⇒ String
Remove extra newlines from the output.
200 201 202 203 |
# File 'lib/random-words/string.rb', line 200 def compress_newlines # Removes extra newlines from the output. gsub(/\n{2,}/, "\n\n").strip end |
#compress_newlines! ⇒ String
Compress newlines in the output, destructive.
208 209 210 211 |
# File 'lib/random-words/string.rb', line 208 def compress_newlines! # Removes extra newlines from the output. replace compress_newlines end |
#dedup_punctuation ⇒ String
Remove duplicate punctuation
116 117 118 |
# File 'lib/random-words/string.rb', line 116 def dedup_punctuation gsub(/((%[A-Z]+%)?[,.;:—] ?)+/, '\1').gsub(/([,;:—])/, '\1') end |
#downcase_first ⇒ String
Downcase the first letter of a string, respecting punctuation.
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/random-words/string.rb', line 80 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 |
#ensure_pipes ⇒ String
Ensure leading and trailing pipes
144 145 146 |
# File 'lib/random-words/table-cleanup.rb', line 144 def ensure_pipes strip.gsub(/^\|?(.*?)\|?$/, '|\1|') end |
#expand_debug(testing: false) ⇒ String
Expand abbreviated debug statements in the string.
344 345 346 347 348 349 350 351 352 |
# File 'lib/random-words/string.rb', line 344 def (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 |
#expansions ⇒ Object
Dictionary of expansions
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/random-words/string.rb', line 316 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 |
#fix_caps(terminators) ⇒ String
Capitalize the first letter of each sentence in a string. Scans for all known terminators followed by a space and capitalizes the next letter.
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/random-words/string.rb', line 98 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 |
#indent(string) ⇒ String
Indent every line in a string with a specified string.
178 179 180 |
# File 'lib/random-words/string.rb', line 178 def indent(string) gsub(/^/, string) end |
#no_term(terminators) ⇒ String
Remove any punctuation mark from the end of a string.
162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/random-words/string.rb', line 162 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 |
#preserve_spaces ⇒ String
Preserve spaces in the output by replacing multiple spaces with %%.
215 216 217 218 |
# File 'lib/random-words/string.rb', line 215 def preserve_spaces # Preserves spaces in the output. gsub(/ +/, '%%') end |
#restore_spaces ⇒ String
Restore spaces in the output by replacing %% with spaces.
222 223 224 225 |
# File 'lib/random-words/string.rb', line 222 def restore_spaces # Restores spaces in the output. gsub('%%', ' ') end |
#split_lines ⇒ Array<String>
Split a string by newlines, clean each line, and remove empty lines.
132 133 134 135 136 |
# File 'lib/random-words/string.rb', line 132 def split_lines arr = strip.split("\n").map(&:clean) arr.reject!(&:empty?) arr end |
#term(char = '.') ⇒ String
Add a specified character to the end of a string, avoiding repeated punctuation.
193 194 195 196 |
# File 'lib/random-words/string.rb', line 193 def term(char = '.') # Adds a specified character to the end of a string. sub(/[.?!,;]?$/, "#{char}") end |
#terminate(terminator) ⇒ String
Terminate a string with a random punctuation mark.
150 151 152 153 154 155 156 157 158 |
# File 'lib/random-words/string.rb', line 150 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 |
#to_length ⇒ Symbol
Convert the string to a length symbol based on its name.
256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/random-words/string.rb', line 256 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 |
#to_sent(terminator) ⇒ String
Generate a sentence with capitalization and terminator
123 124 125 |
# File 'lib/random-words/string.rb', line 123 def to_sent(terminator) capitalize.compress.capitalize_i.dedup_punctuation.terminate(terminator) end |
#to_source ⇒ Symbol
Convert the string to a source symbol based on its name.
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/random-words/string.rb', line 235 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 |
#trueish? ⇒ Boolean
Check if the string is trueish (starts with ‘t’, ‘y’, or ‘1’).
229 230 231 |
# File 'lib/random-words/string.rb', line 229 def trueish? to_s =~ /^[ty1]/i end |