Module: Strings::Fold

Defined in:
lib/strings/fold.rb

Constant Summary collapse

LINE_BREAK =
"(\r\n+|\r+|\n+|\t+)".freeze

Class Method Summary collapse

Class Method Details

.fold(text, separator = LINE_BREAK) ⇒ String

Fold a multiline text into a single line string

Examples:

fold("\tfoo \r\n\n bar") # => " foo  bar"

Parameters:

  • text (String)
  • separator (String) (defaults to: LINE_BREAK)

    the separators to be removed from the text, default: (rn+|r+|n+|t+)

Returns:

  • (String)


20
21
22
23
24
# File 'lib/strings/fold.rb', line 20

def fold(text, separator = LINE_BREAK)
  text.gsub(/([ ]+)#{separator}/, "\\1")
      .gsub(/#{separator}(?<space>[ ]+)/, "\\k<space>")
      .gsub(/#{separator}/, " ")
end