We Recommend

Beginning Ruby: From Novice to Professional Beginning Ruby: From Novice to Professional
Beginning Ruby is a thoroughly contemporary guide for every type of reader wanting to learn Ruby, from novice programmers to web developers to Ruby newcomers. It starts by explaining the principles behind object-oriented programming and within a few chapters builds toward creating a genuine Ruby application.


Posted By

breakerjump on 05/16/08


Tagged

text regular wrap Expression word


Versions (?)


Text Wrapping with Regular Expressions


Published in: Ruby 


URL: http://blog.macromates.com/2006/wrapping-text-with-regular-expressions/

Hat tip: Allan Odgaard


  1. def wrap_text(txt, col = 80)
  2. txt.gsub(/(.{1,#{col}})( +|$)\n?|(.{#{col}})/,
  3. "\\1\\3\n")
  4. end

Report this snippet 

You need to login to post a comment.