On 27 Sep., 20:04, Caleb Clausen <vikk... / gmail.com> wrote:
> On 9/27/09, Ehsanul Hoque <ehsanul... / hotmail.com> wrote:
>
> > Yes, indeed a new object is indeed created every time an anonymous object is
> > created. The only core object I know of for which this is not true is the
> > symbol, which is basically an immutable string.

I think that's not quite what I meant.  Of course, if I define the
same regular expression twice at different places, there would be two
regexp objects.


>
> Actually, I believe that regexp literals are created only once even if
> they're executed multiple times. The exception to this would be when
> you use #{} within a regexp... that forces ruby to not only create a
> new object each time the regexp literal is executed, it has to
> recompile the regexp each time.... and that is really slow. You can
> bypass this behavior by using the o regexp option, but that only works
> right if the value of the inclusion (what's inside #{}) is guaranteed
> to be the same on each execution.
>


Thanks so much!  Your suspicion was right, I am indeed using #{} in
some of the regular expressions, and the o option does fix the issue.
And your explanation why the expressions would otherwise be recompiled
in every iteration is now very obvious to me.

Now my code is already a bit shorter :)!

Thomas W.