[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

code snippet: can it be done better/shorter?

Krekna Mektek

3/1/2006 3:24:00 PM

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 =~ /Checking:/
line =~ /<(.*?)>.*<(.*)>/
from = $1; rcpt = $2
from = "Unknown" if from == ""
rcpt = "Unknown" if rcpt == ""
return ([from,rcpt])
elsif line =~ /ClamAV-clamd\ result:\ /
return $'
else·
return []·
end

end
end

Cheers,
Krekna


3 Answers

dblack

3/1/2006 3:45:00 PM

0

Krekna Mektek

3/1/2006 4:24:00 PM

0

Hi there,

2006/3/1, dblack@wobblini.net <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 =~ /Checking:/
> > line =~ /<(.*?)>.*<(.*)>/
> > from = $1; rcpt = $2
> > from = "Unknown" if from == ""
> > rcpt = "Unknown" if rcpt == ""
> > return ([from,rcpt])
> > elsif line =~ /ClamAV-clamd\ result:\ /
> > return $'
> > else·
> > return []·
> > 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?

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!

Krekna


>
> Here's a slightly different way that might give you some ideas:
>
> class LineChecker
>
> REG=/<(.*?)>.*<(.*)>/
>
> def checkline(line)
> case line
> when /Checking:/
> m = REG.match(line)
> from, rcpt = m.captures if m
> [from || "Unknown", rcpt || "Unknown"]
> when /ClamAV-clamd result: /
> $'
> else
> []
> end
> end
> end
>
>
> David
>
> --
> David A. Black (dblack@wobblini.net)
> Ruby Power and Light (http://www.rubypoweran...)
>
> "Ruby for Rails" chapters now available
> from Manning Early Access Program! http://www.manning.com/b...
>
>


dblack

3/1/2006 4:28:00 PM

0