String substitution bug

M

Martin Schoettler

(Ruby 1.8.1, Linux and Windows)

puts "a\\b".sub( /\\/, "\\\\" )
yields
a\b
(I expected: a\\b)

But
puts "a\\b".sub( /\\/, "\\x\\" )
yields
a\x\b
(as I would expect.)

BTW: Mails to (e-mail address removed) are rejected. Is there a new mail
adress for bugs?
 
K

Kristof Bastiaensen

(Ruby 1.8.1, Linux and Windows)

puts "a\\b".sub( /\\/, "\\\\" )
yields
a\b
(I expected: a\\b)

But
puts "a\\b".sub( /\\/, "\\x\\" )
yields
a\x\b
(as I would expect.)

BTW: Mails to (e-mail address removed) are rejected. Is there a new mail
adress for bugs?

Hi,

I don't know about bugs, but this however is not bug,
but a feature :)
When using a replacement string, gsub does another
elimination step for backslashes, because "\1" and "\2"
have a special meaning. So first "\\\\" will be translated
to "\\" for the quoted string, and the second time it will
be translated to "\" by gsub. In the second case however gsub
will see "\x\", and since "\x" doesn't have a special meaning
it will be kept.

Regards,
Kristof
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,147
Messages
2,570,833
Members
47,380
Latest member
AlinaBlevi

Latest Threads

Top