Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > More better way to split off blank lines

Reply
Thread Tools

More better way to split off blank lines

 
 
trans. (T. Onoma)
Guest
Posts: n/a
 
      10-17-2004
In the following code, write_stack is an array composed of special marker
objects and strings, and I've extended String with the #blank? method. I want
to split any space which includes a newline from the front and back of the
strings and make them their own elements in the array. Note: as the last line
indicates, I don't necessarily need the new_stack, if it can be done just by
modifying the write_stack instead.


# split off blank lines
new_stack = []
write_stack.each_index{ |i|
# if string and not blank
if write_stack[i].respond_to?(:blank?) && ! write_stack[i].blank?
md = /^(\s*\n\s*)(\S.*\S)(\s*\n\s*)$/.match(write_stack[i])
if md
new_stack << md[1] if md[1].length > 0
new_stack << md[2]
new_stack << md[3] if md[3].length > 0
else
new_stack << write_stack[i]
end
else
new_stack << write_stack[i]
end
}
write_stack = new_stack


Is there a nicer way to do this?

Thanks,
T.



 
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
Is a blank at the end of a C file a better style than a newline atthe end of the file or is it better to place none? Jimmy C Programming 3 09-09-2011 10:36 PM
Is there a better/simpler way to filter blank lines? tmallen Python 19 11-05-2008 11:07 PM
Preserve blank lines when add multiple lines of text to a cell Cah Sableng Javascript 0 04-23-2007 04:46 AM
Why does split operate over multiple lines in the absence of "ms" ? And why doesn't $_ work with split? Sara Perl Misc 6 04-12-2004 09:07 AM
split on blank lines Jan Burgy Python 4 12-02-2003 10:32 AM



Advertisments