Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > can it be shorter?

Reply
Thread Tools

can it be shorter?

 
 
WuyaSea Operator
Guest
Posts: n/a
 
      12-11-2006
doing formating, i need to break a long line of words to roughly 80
chars per line, below is what I got

def break_line(text)
return text if text.size < 80

i = text.index(' ', 79)
text[0..i] + "\n" + break_line(text[i+1..-1])
end


can it be any shorter, more rubyish? Thank you.

operator
www.wuyaSea.com

 
Reply With Quote
 
 
 
 
Michael Fellinger
Guest
Posts: n/a
 
      12-11-2006
On Monday 11 December 2006 14:20, WuyaSea Operator wrote:
> doing formating, i need to break a long line of words to roughly 80
> chars per line, below is what I got
>
> def break_line(text)
> return text if text.size < 80
>
> i = text.index(' ', 79)
> text[0..i] + "\n" + break_line(text[i+1..-1])
> end
>
>
> can it be any shorter, more rubyish? Thank you.
>


It's not perfect, but well... just inviting all the regex-gurus to improve
it

^manveru

class String
def linearize(max = 60)
scan(/.{1,#{max}}(?=\s|$)/)
end
end

"The Lojban alphabet consists of the 26 characters".linearize
# ["The Lojban alphabet consists of the 26 characters"]

"The Lojban alphabet consists of the 26 characters".linearize 10
# ["The Lojban", " alphabet", " consists", " of the 26", "characters"]

"The Lojban alphabet consists of the 26 characters".linearize 20
# ["The Lojban alphabet", " consists of the 26", " characters"]

"The Lojban alphabet consists of the 26 characters".linearize 30
# ["The Lojban alphabet consists", " of the 26 characters"]

"The Lojban alphabet consists of the 26 characters".linearize 40
# ["The Lojban alphabet consists of the 26", " characters"]

> operator
> www.wuyaSea.com


 
Reply With Quote
 
 
 
 
James Edward Gray II
Guest
Posts: n/a
 
      12-11-2006
On Dec 10, 2006, at 11:20 PM, WuyaSea Operator wrote:

> doing formating, i need to break a long line of words to roughly 80
> chars per line, below is what I got
>
> def break_line(text)
> return text if text.size < 80
>
> i = text.index(' ', 79)
> text[0..i] + "\n" + break_line(text[i+1..-1])
> end
>
>
> can it be any shorter, more rubyish? Thank you.


def break_line(text)
text.gsub!(/(.{1,80}|\S{81,})(?: +|$\n?)/, "\\1\n")
end

James Edward Gray II


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can Groovy be used in an applet and/or can it generate the Java bytecodes that then can be used in an applet? Casey Hawthorne Java 1 03-18-2009 12:56 AM
Word Docs Won't Open, Can't Be E-Mailed, Can't Be Deleted, Can't Be Copied, Etc. Martin Computer Support 16 02-24-2009 07:35 PM
Wireless can get internet but can't see network -- can when wired 02befree Computer Support 0 12-24-2007 09:10 PM
SOLVED - can't open file in windows media player / WMP. But can in VLC - video LAN .. Now can in WMP jameshanley39@yahoo.co.uk Computer Information 2 09-19-2007 02:53 AM
Windows can see mapped drives, but applications can't? =?Utf-8?B?RGFuaWVsIEVpY2hvcm4=?= Wireless Networking 3 11-18-2004 11:03 PM



Advertisments