Sha256: e7b95eb40b2842928be99eb9069866a90c9e4165d4ea426a290855e55cc68cb7

Contents?: true

Size: 665 Bytes

Versions: 10

Compression:

Stored size: 665 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, 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 10 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/string/word_wrap.rb
facets-2.8.3 lib/core/facets/string/word_wrap.rb
facets-2.8.2 lib/core/facets/string/word_wrap.rb
facets-2.8.1 lib/core/facets/string/word_wrap.rb
facets-2.8.0 lib/core/facets/string/word_wrap.rb
facets-2.7.0 lib/core/facets/string/word_wrap.rb
facets-2.6.0 lib/core/facets/string/word_wrap.rb
facets-2.5.1 lib/core/facets/string/word_wrap.rb
facets-2.5.0 lib/core/facets/string/word_wrap.rb
facets-2.5.2 lib/core/facets/string/word_wrap.rb