Sha256: 9e67b67e8152082c38f033208d34a331e2e0dce7b42a32f4c3607a9300b7b124

Contents?: true

Size: 664 Bytes

Versions: 10

Compression:

Stored size: 664 Bytes

Contents

class String

  # Word wrap a string not exceeding max width.
  #
  #   "this is a test".word_wrap(4)
  #
  # _produces_ ...
  #
  #   this
  #   is a
  #   test
  #
  # This is basic implementation of word wrap, but smart
  # enough to suffice for most use cases.
  #
  # CREDIT: Gavin Kistner, Dayne Broderson
  #
  def word_wrap( col_width=80 )
    self.dup.word_wrap!( col_width )
  end

  # As with #word_wrap, but modifies the string in place.
  #
  # CREDIT: Gavin Kistner, Dayne Broderson
  #
  def word_wrap!( col_width=80 )
    self.gsub!( /(\S{#{col_width}})(?=\S)/, '\1 ' )
    self.gsub!( /(.{1,#{col_width}})(?:\s+|$)/, "\\1\n" )
    self
  end

end

Version data entries

10 entries across 9 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/string/word_wrap.rb
facets-3.1.0 lib/core/facets/string/word_wrap.rb
facets-3.0.0 lib/core/facets/string/word_wrap.rb
facets-2.9.3 lib/core/facets/string/word_wrap.rb
facets-2.9.2 src/core/facets/string/word_wrap.rb
facets-2.9.2 lib/core/facets/string/word_wrap.rb
facets-2.9.1 lib/core/facets/string/word_wrap.rb
facets-2.9.0 lib/core/facets/string/word_wrap.rb
facets-2.9.0.pre.2 lib/core/facets/string/word_wrap.rb
facets-2.9.0.pre.1 lib/core/facets/string/word_wrap.rb