Sha256: 11d2cf4c669348fd52656c04e0c544858ca3913b47b06803243651b28888cdc1

Contents?: true

Size: 693 Bytes

Versions: 4

Compression:

Stored size: 693 Bytes

Contents

class String

  # Word wrap a string not exceeding max width.
  #
  #   puts "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
  #  CREDIT: 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
  #   CREDIT: 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

4 entries across 4 versions & 2 rubygems

Version Path
facets-2.4.3 lib/core/facets/string/word_wrap.rb
facets-2.4.4 lib/core/facets/string/word_wrap.rb
facets-2.4.5 lib/core/facets/string/word_wrap.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/core/facets/string/word_wrap.rb