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.rubypowerandlight.com)
>
> "Ruby for Rails" chapters now available
> from Manning Early Access Program! http://www.manning.com/books/black
>
>