On 7/31/05, Garance A Drosehn <drosihn / gmail.com> wrote:
>  If that case statement is going to be
> processed many times, then I like to define objects via
> Regexp.new, and use those pre-compiled objects in the
> when clauses:
> 
>     re_simple = Regexp.new("check (\d+)")
>     ...
>     very_huge_file.each_line { |aline}
>         case aline
>             when re_simple
>                 check_number = $1
>             when re_other
>        ...
> 

Why? I don't think you have to do this to avoid recompiling each time.
Ruby should compile it once when the program is first parsed, and then
recompiles are not needed (unless your regexp has an interpolation).

just so long as you do this:

  when /some_rex/:

and not this:

  when Regexp.new("some_rex"):


> I was kinda wondering if it would make sense for ruby to
> support something like:
> 
>     re_simple = Regexp.new("check (\d+)") { cnum = $1 }

Ah! a man after my own heart. I think this would be just lovely. It's
never quite so simple, tho. What about, eg re_simple.to_s?

You might want to look at my Reg library/pattern matching language. If
I ever finish the required features, it will support things like what
you have above. Not quite the same syntax, it'd look more like:

  re_simple=/check (\d+)/>>BR[1]

and then

  case str
  when re_simple: check_number=str

Ok, that probably makes no sense to anyone but me yet.