More better way to split off blank lines

T

trans. (T. Onoma)

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.respond_to?:)blank?) && ! write_stack.blank?
md = /^(\s*\n\s*)(\S.*\S)(\s*\n\s*)$/.match(write_stack)
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
end
else
new_stack << write_stack
end
}
write_stack = new_stack


Is there a nicer way to do this?

Thanks,
T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top