From: Joseph McDonald Date: 2002-09-24T09:01:49+09:00 Subject: ruby refactoring Hi, I'd like to split a message up into unquoted parts and quoted parts (contiguous lines that start with a /^\s*>/ are considered "quoted text"). If there are multiple quoted and unquoted parts, I need them in order in an array. The code below does what I need, but it seems awfully long and convoluted for what it does. I think I may be approaching it wrong. Any ideas on how to make it better? thanks, -joe #!/usr/local/bin/ruby def make_parts(msg) unquoted_data = "" quoted_data = "" msg_parts = [] last_type = nil msg.split("\n").each do |line| next if line.length == 0 if line =~ /^\s*\>/ if last_type == "unquoted" || last_type == nil # this is a new quoted section if unquoted_data.length > 0 words = unquoted_data.split msg_parts << ["unquoted",words] if words.length unquoted_data = "" end end last_type = "quoted" quoted_data += "#{line}\n" else # this is unquoted text if last_type == "quoted" || last_type == nil # this is a new unquoted section if quoted_data.length > 0 words = quoted_data.split msg_parts << ["quoted",words] if words.length quoted_data = "" end end last_type = "unquoted" unquoted_data += "#{line}\n" end end # get the last one. if last_type == "unquoted" if unquoted_data.length > 0 words = unquoted_data.split msg_parts << ["unquoted",words] if words.length end else if quoted_data.length > 0 words = quoted_data.split msg_parts << ["quoted",words] if words.length end end msg_parts end if __FILE__ == $0 msg=<<'EOF' This is unquoted text and more > This is quoted text > and more and more unquoted text blah >blah EOF p make_parts(msg) end