Sha256: e32acbdc5c42982d9723dd4476a83bdf75dbb99794cfadbc909628a98531d7a6

Contents?: true

Size: 777 Bytes

Versions: 17

Compression:

Stored size: 777 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. "
  #
  # 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
    br = 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

17 entries across 16 versions & 1 rubygems

Version Path
facets-2.9.3 lib/core/facets/string/fold.rb
facets-2.9.2 lib/core/facets/string/fold.rb
facets-2.9.2 src/core/facets/string/fold.rb
facets-2.9.1 lib/core/facets/string/fold.rb
facets-2.9.0 lib/core/facets/string/fold.rb
facets-2.9.0.pre.2 lib/core/facets/string/fold.rb
facets-2.9.0.pre.1 lib/core/facets/string/fold.rb
facets-2.8.4 lib/core/facets/string/fold.rb
facets-2.8.3 lib/core/facets/string/fold.rb
facets-2.8.2 lib/core/facets/string/fold.rb
facets-2.8.1 lib/core/facets/string/fold.rb
facets-2.8.0 lib/core/facets/string/fold.rb
facets-2.7.0 lib/core/facets/string/fold.rb
facets-2.6.0 lib/core/facets/string/fold.rb
facets-2.5.1 lib/core/facets/string/fold.rb
facets-2.5.0 lib/core/facets/string/fold.rb
facets-2.5.2 lib/core/facets/string/fold.rb