On 27 Sep., 21:57, Gary Wright <gwtm... / mac.com> wrote:
> On Sep 27, 2009, at 11:50 AM, ThomasW wrote:
>
> > I'm parsing text files and am using a lot of regexps for this.
> > Initially I was doing something like this:
>
> > file.each_line { |line|
> > =A0if line =3D~ /^pattern[a]*/
> > =A0 =A0process_pattern_a(line)
> > =A0elsif line =3D~ /pat+e(rn)? b\s*$/
> > =A0 =A0process_pattern_b(line)
> > =A0# some more elsifs
> > =A0end
> > }
>
> This example is perfect for Ruby's case statement:
>
> file.each_line { |line|
> =A0 =A0case line
> =A0 =A0when /^pattern[a]*/o
> =A0 =A0 process_pattern_a(line)
> =A0 =A0when /pat+e(rn)? b\s*$/o
> =A0 =A0 process_pattern_b(line)
> =A0 =A0# more when clauses
> =A0 =A0else
> =A0 =A0 =A0# handle no match
> =A0 =A0end
>
> }
>
> Gary Wright

Thanks for that tip.  I wasn't aware that this also works with regexp
matches.  It's great that it does!  By the way, is there anything
substantially different from an elsif chain, except for being slightly
less typing?

Thomas W.