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