From: "Peña, Botp" Date: 2008-05-30T10:16:45+09:00 Subject: Re: Strings, postgresql and gsub From: Robert Klemme [mailto:shortcutter@googlemail.com] # 2008/5/29 Peña, Botp : # > faq. try the block form for less headache. # # ... and less performance. by how much? Benchmark.realtime{9999.times{s.gsub(/'/){"\\'"}}} #=> 0.139999866485596 Benchmark.realtime{9999.times{s.gsub(/'/,"\\\\'")}} #=> 0.108999967575073 # ... Proper escaping is the way to go. 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 heavier. "Proper" is relative, pick one that works best for you :) puts "\\\\'" \\' #=> nil puts "\\'" \' i'd wish ruby2 would do s.gsub(/'/)("\\'") #=> "blah\\'blah" ie, backreferences and company should use the more flexible block form s.gsub(/'/){"\\'"} #=> "blah\\'blah" kind regards -botp