Sha256: a9b801d3aed26d9758e35a227d5d39a2bd23a7ad21a3c8798f6bece8377567bf

Contents?: true

Size: 847 Bytes

Versions: 4

Compression:

Stored size: 847 Bytes

Contents

class String
  # Returns a new string with all new lines removed from 
  # adjacent lines of text.
  #
  #   require 'facet/string/clump'
  #
  #   s = "This is\na test.\n\nIt clumps\nlines of text."
  #   s.clump
  #
  # _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 clump
    ns = ''
    i = 0
    br = self.scan(/(\n\s*\n|\Z)/m) { |m|
      b = $~.begin(1)
      e = $~.end(1)
      nl = $&
      ns << self[i...b].gsub(/[ ]*\n+/,' ')
      ns << nl
      i = e
    }
    ns
  end
end 


=begin
# --- dev testing ---
if $0 == __FILE__

s = <<-EOS
This is a test of
what I am trying to
do here.

I hope it is easy
peasy as can beasy.
EOS

s.clump

end
=end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
facets-0.6.3 lib/facet/string/clump.rb
facets-0.7.0 lib/facet/string/clump.rb
facets-0.7.1 lib/facet/string/clump.rb
facets-0.7.2 lib/facet/string/clump.rb