Sha256: 1913c1d40d73413c9db9a42710bdc36562b4e13d6ac8688d05b229b3ffeb406b

Contents?: true

Size: 1.36 KB

Versions: 26

Compression:

Stored size: 1.36 KB

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 possible flaw with this that might could use a fix: 
  # if the given string ends in a newline, it is replaced with
  # a single space.
  #++
  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


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TCString < Test::Unit::TestCase

    def test_fold_1
      s = "This is\na test.\n\nIt clumps\nlines of text."
      o = "This is a test.\n\nIt clumps lines of text."
      assert_equal( o, s.fold )
    end

    def test_fold_2
      s = "This is\na test.\n\n  This is pre.\n  Leave alone.\n\nIt clumps\nlines of text."
      o = "This is a test.\n\n  This is pre.\n  Leave alone.\n\nIt clumps lines of text."
      assert_equal( o, s.fold(true) )
    end

  end

=end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
facets-0.9.0 lib/nano/string/fold.rb
facets-1.0.0 lib/facet/string/fold.rb
facets-1.0.3 packages/core/lib/facet/string/fold.rb
facets-1.2.1 lib/facets/core/string/fold.rb
facets-1.1.0 lib/facet/string/fold.rb
facets-1.3.0 lib/facets/core/string/fold.rb
facets-1.2.0 lib/facets/core/string/fold.rb
facets-1.3.3 lib/facets/core/string/fold.rb
facets-1.3.1 lib/facets/core/string/fold.rb
facets-1.3.2 lib/facets/core/string/fold.rb
facets-1.4.0 lib/facets/core/string/fold.rb
facets-1.4.1 lib/facets/core/string/fold.rb
facets-1.4.2 lib/facets/core/string/fold.rb
facets-1.4.3 lib/facets/core/string/fold.rb
facets-1.4.5 lib/facets/core/string/fold.rb
facets-1.4.4 lib/facets/core/string/fold.rb
facets-1.7.38 lib/facets/core/string/fold.rb
facets-1.7.30 lib/facets/core/string/fold.rb
facets-1.7.0 lib/facets/core/string/fold.rb
facets-1.7.46 lib/facets/core/string/fold.rb