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 clean
-
(String)
- The cleaned string. -
(String)
- The string with unwanted characters removed.
def clean gsub(/[^-a-zA-Z0-9\s]/, '').strip end
def compress
-
(String)
- The string with extra spaces compressed.
def compress gsub(/\s+/, ' ').strip end
def no_term
-
(String)
- The string with the last punctuation mark removed.
def no_term sub(/[.!?;,-]*$/, '') end
def split_lines
-
(Array
- The string split into lines, cleaned, and empty lines removed.)
def split_lines strip.split("\n").map(&:clean).reject(&:empty?) end
def terminate
-
(String)
- The string with a random punctuation mark at the end.
def terminate terminators = %w[. . . ! ! ?] terminator = terminators.sample sub(/[.!?;,-]*$/, terminator) end