T. Onoma
10/17/2004 4:12:00 PM
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.