Gsub() replacement containing replaced text

This feels like a terribly newbie question to be asking, but I’m stymied …

I want to replace all single quotes in a string with backslask followed by a
single quote. The obvious approach doesn’t seem to work, presumably because
the replacement contains the pattern that’s been replaced.

I assume there’s an easy way to do this, but can’t for the life of me think
how. Here’s an example of the problem …

irb(main):001:0> x=“Don’t say goodbye”
“Don’t say goodbye"
irb(main):002:0> x
"Don’t say goodbye"
irb(main):003:0> x.gsub(”’", “\’”)
"Dont say goodbyet say goodbye"
irb(main):004:0>

Harry Ohlsen wrote:

I want to replace all single quotes in a string with backslask followed by a
single quote. The obvious approach doesn’t seem to work, presumably because
the replacement contains the pattern that’s been replaced.

One way (learned this in a post here a few days ago):

irb(main):001:0> “foo’bar”.gsub(“'”, “\'”)
“foobarbar”
irb(main):002:0> “foo’bar”.gsub(“'”) {“\'”}
“foo\'bar”

This issue is mentioned in ‘Programming Ruby’, “Standard Types” chapter
(see http://www.rubycentral.com/book/).

As I said, I figured it was a bit of a newbie question :-). I read PR chapter
18 months ago, but presumably haven’t needed to use that specific piece of
information up until now … and that my brain doesn’t have enough spare
capacity :-).

The problem is that you can use &, \1 \2 etc. in the replacement string to
match groups, so gsub is gobbling up your \.

That makes sense.

The solution is to either do
x.gsub(“'”, “\\'”)
or
x.gsub(“'”) { “\'” }

The latter form works because when given a block, groups are given as
arguments to the block instead of using \1, \2 etc, so gsub doesn’t have to
touch 's in the replacement string.

Thanks. I’ll go and re-read the section of PR you mentioned, anyway.

Cheers.

Hi,

···

In message “gsub() replacement containing replaced text” on 02/07/13, Harry Ohlsen harryo@zip.com.au writes:

I want to replace all single quotes in a string with backslask followed by a
single quote. The obvious approach doesn’t seem to work, presumably because
the replacement contains the pattern that’s been replaced.

“\'” in a replacement string is substitute by the value of $', so you
have to say

x.gsub(“'”, “\\'”)

						matz.

Would it make sense to add this to the Ruby FAQ?

On a related note, I have been reading the Programming Ruby book and there
is a pretty good section on gotchas that might also be good for the FAQ.
Not sure how the authors feel about having the information in both places?
Maybe have a link to the PR section from the FAQ?

John.

“Yukihiro Matsumoto” matz@ruby-lang.org wrote in message
news:1026541001.000220.29303.nullmailer@picachu.netlab.jp…

Hi,

I want to replace all single quotes in a string with backslask followed
by a
single quote. The obvious approach doesn’t seem to work, presumably
because

···

In message “gsub() replacement containing replaced text” > on 02/07/13, Harry Ohlsen harryo@zip.com.au writes:

the replacement contains the pattern that’s been replaced.

“\'” in a replacement string is substitute by the value of $', so you
have to say

x.gsub(“'”, “\\'”)

matz.

OK, I just found the wiki page where you can make suggestions for the FAQ, I
will do this on that page.

As an aside, could we make the FAQ itself a wiki? That way people could
update it themselves. I have yet to see anarchy with a wiki - they have all
been well behaved.

I would even volunteer moving the current FAQ over to the wiki if people
liked the idea.

John.

“John” nojgoalbyspam@hotmail.com wrote in message
news:9w_X8.10957$Kx3.6313@newsread1.prod.itd.earthlink.net

···

Would it make sense to add this to the Ruby FAQ?

On a related note, I have been reading the Programming Ruby book and there
is a pretty good section on gotchas that might also be good for the FAQ.
Not sure how the authors feel about having the information in both places?
Maybe have a link to the PR section from the FAQ?

John.

“Yukihiro Matsumoto” matz@ruby-lang.org wrote in message
news:1026541001.000220.29303.nullmailer@picachu.netlab.jp…

Hi,

In message “gsub() replacement containing replaced text” > > on 02/07/13, Harry Ohlsen harryo@zip.com.au writes:

I want to replace all single quotes in a string with backslask followed
by a
single quote. The obvious approach doesn’t seem to work, presumably
because
the replacement contains the pattern that’s been replaced.

“\'” in a replacement string is substitute by the value of $', so you
have to say

x.gsub(“'”, “\\'”)

matz.