From: Mason Kelsey Date: 2009-09-24T11:47:31+09:00 Subject: Re: How do you limit the line length of the output commands? Where is pqueue library documented? --001636426873033388047449dae9 Content-Type: text/plain; charset=ISO-8859-1 Oh! This less code to write even if it leans heavily on knowing how to do regular expressions, which I am still learning. Very nice. But you made a minor typo error in your command puts pq.methods.join(", ").word_wrap which should have been puts pq.methods.join(", ").word_wrap(50) where the 50 is the max. length of the line. Much thanks, Good Code! I tested it for several different lengths. No Sam On Wed, Sep 23, 2009 at 10:29 PM, Justin Collins wrote: > Caleb Clausen wrote: > >> On 9/23/09, Patrick Okui wrote: >> >> >>> On 9/24/09 12:59 AM, Mason Kelsey wrote: >>> >>> >>>> First Question: What is an easy way to modify the below puts code to >>>> control >>>> line length without truncation? >>>> >>>> >>> [snip] >> >> >>> If that doesn't satisfy you I'd guess you're stuck with stuffing the >>> methods into the end of a buffer yourself. (sorry this feels more like C >>> than ruby to this ruby newbie but I can't think of a simpler way) >>> >>> buffer = '' >>> pq.methods.each do |m| >>> if buffer.split("\n")[-1].to_s.length + m.length < 60 >>> buffer << ((buffer.length > 0)? ", #{m}": "#{m}") >>> else >>> buffer << "\n#{m}" >>> end >>> end >>> >>> puts buffer >>> >>> >> >> There's a way to do this using Regexp and gsub, but I almost always >> mess it up when I try to code it off the top of my head. It seems to >> be difficult enough that its actually better to do it like you have >> above, even tho the regexp version is only like 2 lines >> > > This is a pretty decent one I've been using: > > class String > def word_wrap length > self.gsub(/([^\r\n\n\s\Z]{#{length}})/, "\\1 > ").gsub(/(.{1,#{length}})(\r\n|\n|\s+|\Z)/, "\\1\n") > end > end > > puts pq.methods.join(", ").word_wrap > > > -Justin > > --001636426873033388047449dae9--