From: Charles Hixson Date: 2013-04-19T10:36:21+09:00 Subject: Re: Match against multiple patterns problem This is a multi-part message in MIME format. --------------020103060900090401070801 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 04/18/2013 11:17 AM, Wayne Brisette wrote: > Wouldn't using the case statement be a better option than all the > elsif statements? Seems like this is the perfect situation to use case. > > Wayne > The case expression might be a bit cleaner, but that would appear to coerce using the $~ pattern match value, which I don't like at all. So I think I'll stick to cleaning up the typo, and changing len(s) to s.length. Then I'll merge the initial if onto the rest, so everything is the same form. I'm glad I hadn't misremembered how the assignment should be written, but the error messages sure indicated (to me) that I had. Now that the problem is explained I've cleared it up a lot. (E.g., those != nil things turned out to be superfluous.) The current version is: if not s.is_a?(String) or s.length < 1 then return ["1",""] if (m = /^[^\W_]+/.match(s) ) then return ["a", m[0]] if (m = /^\s+/.match(s) ) then return ["s", m[0]] if (m = /^[[:cntrl:]]+/) then return ["c", m[0]] if (m = /^[[:punct:]_]+/) then return ["p", m[0]] if (m = /^[^\w\s[:cntrl:][:punct:]]+/) != nil then return ["r", m[0]] which looks a lot better. -- Charles Hixson --------------020103060900090401070801 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit On 04/18/2013 11:17 AM, Wayne Brisette wrote:
Wouldn't using the case statement be a better option than all the elsif statements? Seems like this is the perfect situation to use case. 

Wayne

The case expression might be a bit cleaner, but that would appear to coerce using the $~ pattern match value, which I don't like at all.  So I think I'll stick to cleaning up the typo, and changing len(s) to s.length.  Then I'll merge the initial if onto the rest, so everything is the same form.

I'm glad I hadn't misremembered how the assignment should be written, but the error messages sure indicated (to me) that I had.  Now that the problem is explained I've cleared it up a lot.  (E.g., those != nil things turned out to be superfluous.)  The current version is:
        if    not s.is_a?(String)    or    s.length < 1    then    return    ["1",""]
        if    (m = /^[^\W_]+/.match(s) )    then    return    ["a", m[0]]
        if    (m = /^\s+/.match(s) )        then    return    ["s", m[0]]
        if    (m = /^[[:cntrl:]]+/)         then    return    ["c", m[0]]
        if    (m = /^[[:punct:]_]+/)        then    return    ["p", m[0]]
        if    (m = /^[^\w\s[:cntrl:][:punct:]]+/) != nil
                                            then    return    ["r", m[0]]

which looks a lot better.

-- 
Charles Hixson
--------------020103060900090401070801--