Sha256: f72cc513c122869906e13f8a117e5da8db4a0ff9ce5913774337cdb27b9b7a73

Contents?: true

Size: 783 Bytes

Versions: 3

Compression:

Stored size: 783 Bytes

Contents

class String

  # Returns a new string with all new lines removed from
  # adjacent lines of text.
  #
  #     s = "This is\na test.\n\nIt clumps\nlines of text."
  #     s.fold
  #
  # _produces_
  #
  #     "This is a test.\n\nIt clumps lines of text. "
  #
  # TODO: One arguable flaw with this that might need a fix:
  # if the given string ends in a newline, it is replaced with
  # a single space.
  #
  # CREDIT: Trans

  def fold(ignore_indented=false)
    ns = ''
    i = 0
    self.scan(/(\n\s*\n|\Z)/m) do |m|
      b = $~.begin(1)
      e = $~.end(1)
      nl = $&
      tx = slice(i...b)
      if ignore_indented and slice(i...b) =~ /^[ ]+/
        ns << tx
      else
        ns << tx.gsub(/[ ]*\n+/,' ')
      end
      ns << nl
      i = e
    end
    ns
  end

end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/string/fold.rb
facets-3.1.0 lib/core/facets/string/fold.rb
facets-3.0.0 lib/core/facets/string/fold.rb