code snippet: can it be done better/shorter?

K

Krekna Mektek

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

Cheers,
Krekna
 
D

dblack

--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 --

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 ([email protected])
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--
 
K

Krekna Mektek

Hi there,

2006/3/1 said:
Hi --

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?

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=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

--
David A. Black ([email protected])
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
 
D

dblack

--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 --

Hi there,

2006/3/1 said:
Hi --

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 ([email protected])
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--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top