From: dblack@... Date: 2006-03-02T01:28:01+09:00 Subject: Re: code snippet: can it be done better/shorter? --8323328-1444565529-1141230465=:25377 Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-1444565529-1141230465=:25377" 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-1444565529-1141230465=:25377 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Thu, 2 Mar 2006, Krekna Mektek wrote: > Hi there, > > 2006/3/1, dblack@wobblini.net : >> 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 > > This looks indeed beautiful. Hmm, let me see, I can't find the capture > method of the Regexp class in the language reference on rubycentral? It's newer than that language ref. > What does this one do? > > [from || "Unknown", rcpt || "Unknown"] > > Does this mean that if from is empty, but defined, that it returns to > false, and then "Unknown" is chosen? Hah, neat! Actually an empty string is "true" in the Boolean sense, so this idiom depends on from and rcpt being nil -- which they will be, if the match fails. 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-1444565529-1141230465=:25377-- --8323328-1444565529-1141230465=:25377--