On 30.05.2008 03:16, Pe=F1a wrote:
> From: Robert Klemme [mailto:shortcutter / googlemail.com]=20
> # 2008/5/29 Pe=F1a, Botp <botp / delmonte-phil.com>:
> # > faq. try the block form for less headache.
> #=20
> # ... and less performance.
>=20
> by how much?
>=20
> Benchmark.realtime{9999.times{s.gsub(/'/){"\\'"}}}
> #=3D> 0.139999866485596
> Benchmark.realtime{9999.times{s.gsub(/'/,"\\\\'")}}
> #=3D> 0.108999967575073

robert@fussel ~
$ ruby -r benchmark <<XXX
 > Benchmark.bmbm do |r|
 > r.report "block" do
 > 1_000_000.times { "foobarbaz".gsub(/o/){'X'} }
 > end
 > r.report "str" do
 > 1_000_000.times { "foobarbaz".gsub(/o/,'X') }
 > end
 > end
 > XXX
Rehearsal -----------------------------------------
block  10.438000   0.000000  10.438000 ( 10.583000)
str     5.109000   0.000000   5.109000 (  5.310000)
------------------------------- total: 15.547000sec

             user     system      total        real
block  10.578000   0.000000  10.578000 ( 10.816000)
str     5.219000   0.000000   5.219000 (  5.326000)

robert@fussel ~
$

Similar results for 1.8 and 1.9.

> # ... Proper escaping is the way to go.
>=20
> yeh, while losing flexibility/easeofuse :(  that's what's giving me the=
 headache (especially now that i have four eyes and my head's getting hea=
vier. "Proper" is relative, pick one that works best for you :)

My reasoning is as follows: you need to use the block form if you need=20
to _calculate_ the replacement string for every match.  If the=20
replacement follows a particular pattern or is constant use the string fo=
rm.

Kind regards

	robert