[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

setting string width

hurcan solter

1/14/2008 9:00:00 AM

Hi there,Just a quick question ...
I'd like to insert newlines into a string around every 80ish
character and those newlines shouldnt cut the words in the middle. Can
anybody post a snippet how to do that
TIA
hurcan
7 Answers

Andrew Timberlake

1/14/2008 9:41:00 AM

0

Hurcan

The following adds a method to the string class called wrap which wraps a
string at the specified width broken on a space but honouring line endings

class String
def wrap(width)
gsub(/(.{1,#{width}})(\s+|\Z)/, "\\1\n")
end
end

s = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas
cursus cursus enim. Ut sit amet metus at lacus consectetuer condimentum.
Suspendisse nulla arcu, varius eget, consequat id, sodales sit amet, turpis.
Etiam ornare felis id augue. Morbi eleifend sem nec odio. Aenean augue dui,
placerat et, porta tincidunt, accumsan vitae, tortor. Ut et nulla et diam
gravida eleifend. Nulla fermentum ligula nec tellus. Donec quis purus id
nisl blandit fermentum. Nunc commodo metus sit amet tortor. Nam lacus quam,
ultricies sit amet, fermentum at, dictum eget, metus."

puts s.wrap(80)

Andrew Timberlake
andrew@andrewtimberlake.com
082 415 8283
skype: andrewtimberlake

"I have never let my schooling interfere with my education."
--Mark Twain


-----Original Message-----
From: hurcan solter [mailto:hsolter@gmail.com]
Sent: 14 January 2008 11:01 AM
To: ruby-talk ML
Subject: setting string width

Hi there,Just a quick question ...
I'd like to insert newlines into a string around every 80ish
character and those newlines shouldnt cut the words in the middle. Can
anybody post a snippet how to do that
TIA
hurcan


!DSPAM:3,478b262e183241124252716!





Bill Kelly

1/14/2008 9:44:00 AM

0


From: "hurcan solter" <hsolter@gmail.com>
>
> Hi there,Just a quick question ...
> I'd like to insert newlines into a string around every 80ish
> character and those newlines shouldnt cut the words in the middle. Can
> anybody post a snippet how to do that

Here's one way to do it:

string.gsub(/(.{75,}?\S*)\s+/, "\\1\n")

It meets the criteria of "around every 80ish character"... :-)


Regards,

Bill



Xavier Noria

1/14/2008 9:52:00 AM

0

On Jan 14, 2008, at 10:00 AM, hurcan solter wrote:

> I'd like to insert newlines into a string around every 80ish
> character and those newlines shouldnt cut the words in the middle. Can
> anybody post a snippet how to do that

require 'facets'
str.word_wrap!


Bill Kelly

1/14/2008 10:18:00 AM

0


From: "Andrew Timberlake" <andrew@andrewtimberlake.com>
>
> gsub(/(.{1,#{width}})(\s+|\Z)/, "\\1\n")

Ah, nice.


Regards,

Bill




hurcan solter

1/14/2008 11:33:00 AM

0

yes yes the word was "the word wrap" .Silly me
thanks for the responses though..

Robert Dober

1/14/2008 11:45:00 AM

0

On Jan 14, 2008 11:18 AM, Bill Kelly <billk@cts.com> wrote:
>
> From: "Andrew Timberlake" <andrew@andrewtimberlake.com>
> >
> > gsub(/(.{1,#{width}})(\s+|\Z)/, "\\1\n")
>
> Ah, nice.
>
I know you have been served well but I would like to take advantage to
point to a RubyQuiz as there are surprisingly often
answers to Ruby programming questions. Yours was handled here
http://rubyquiz.com/qu...

BTW James, will the Quiz page remain available after your retierement?

Cheers
Robert

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

James Gray

1/14/2008 1:20:00 PM

0

On Jan 14, 2008, at 5:45 AM, Robert Dober wrote:

> BTW James, will the Quiz page remain available after your retierement?

Absolutely.

James Edward Gray II