Dave Thomas <Dave / thomases.com> wrote:
>"Ben Tilly" <ben_tilly / hotmail.com> writes:
>
> > The idea is that a tied wrapper around a regular string
> > like this should be easy to produce and destroy.  Then
> > this lets you be lazy about producing $' and $`, which means
> > that if you don't use it you don't pay for it.  Good old
> > lazy evaluation. :-)
>
>True 'nuf, but remember. In Ruby, $' and $` are simply aliases to the
>pre_match and post_match methods on the MatchData object returned by a
>pattern match. There's no need for ties, as $` and $' are already fetched
>by method calls, and hence can be evaluated lazily or whatever.

OK, this has me curious.  How was this done internally?

From a highly unscientific test it appears that if you
don't use $` and $' in a particular match, you don't pay
for it.  So it looks like it is lazy.  But when I try to
modify the string before having to work out $` and $' I
am unable to find a way to mess up $' and $`.  (Which is
the main risk in lazy evaluation.)  I also note a major
performance drop when I throw in any reference to $' or
$`, and likewise when I try to modify the string (for
instance by appending "").

Now what I thought would require some tying logic would
be to have Ruby understand that it really needs to keep
a copy of the matched string around copy-on-write.  But
judging from my inability to cause a problem and the
performance, it seems that this is already implemented.

Nicely done. :-)

Cheers,
Ben
_____________________________________________________________________________________
Get more from the Web.  FREE MSN Explorer download : http://explorer.msn.com