From: dblack@... Date: 2006-03-02T00:44:34+09:00 Subject: Re: code snippet: can it be done better/shorter? --8323328-1258920798-1141227858=:5699 Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-1258920798-1141227858=:5699" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --8323328-1258920798-1141227858=:5699 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Thu, 2 Mar 2006, Krekna Mektek wrote: > AFAIC See, this works all right, but my question is, just to learn the > Ruby style, can this be done shorter (better)? > > class LineChecker > def checkline(line) > if line =3D~ /Checking:/ > line =3D~ /<(.*?)>.*<(.*)>/ > from =3D $1; rcpt =3D $2 > from =3D "Unknown" if from =3D=3D "" > rcpt =3D "Unknown" if rcpt =3D=3D "" > return ([from,rcpt]) > elsif line =3D~ /ClamAV-clamd\ result:\ / > return $' > else=B7 > return []=B7 > end > > end > end Here's a slightly different way that might give you some ideas: class LineChecker REG=3D/<(.*?)>.*<(.*)>/ def checkline(line) case line when /Checking:/ m =3D REG.match(line) from, rcpt =3D m.captures if m [from || "Unknown", rcpt || "Unknown"] when /ClamAV-clamd result: / $' else [] end end end David --=20 David A. Black (dblack@wobblini.net) Ruby Power and Light (http://www.rubypowerandlight.com) "Ruby for Rails" chapters now available from Manning Early Access Program! http://www.manning.com/books/black --8323328-1258920798-1141227858=:5699-- --8323328-1258920798-1141227858=:5699--