From: Justin Collins Date: 2009-09-24T11:29:19+09:00 Subject: Re: How do you limit the line length of the output commands? Where is pqueue library documented? 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