From: dblack@... Date: 2002-09-24T11:12:50+09:00 Subject: Re: ruby refactoring Hi -- On Tue, 24 Sep 2002, Joseph McDonald wrote: > > 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? Posting the version we've been talking about on #ruby-lang. For those not reading that discussion: one quirky thing about this version is that if you replace the "if" in the middle of the method with "unless", you get exactly the same results (at least with the admittedly ragtag bunch of test cases I ran it on). This is an effect of the fact that if that test is false, midquote gets flipped (in the else clause), so the if/unless and else clauses affect each other. Anyway, here it is: def make_parts(msg) midquote = false buf = [] msg.each do |line| spline = line.strip.split(/\s+/) next if spline.empty? quote = /^\s*>/.match(line) tag = if not quote then "un" end .to_s + "quoted" if (midquote ^ quote) buf << [ tag,[] ] if buf.empty? buf[-1][1].concat spline else buf.concat [ [tag,spline] ] midquote = !midquote end end buf end David -- David Alan Black | Register for RubyConf 2002! home: dblack@candle.superlink.net | November 1-3 work: blackdav@shu.edu | Seattle, WA, USA Web: http://pirate.shu.edu/~blackdav | http://www.rubyconf.com