String substitution bug

(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 ruby-bugs@ruby-lang.org are rejected. Is there a new mail adress for bugs?

    puts "a\\b".sub( /\\/, "\\\\" )

This is not a bug, see

   http://www.rubycentral.com/faq/rubyfaq-9.html#ss9.18

Guy Decoux

Hi,

I don't know about bugs, but this however is not bug,
but a feature :slight_smile:
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

···

On Tue, 29 Jun 2004 14:16:09 +0200, Martin Schoettler wrote:

(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 ruby-bugs@ruby-lang.org are rejected. Is there a new mail
adress for bugs?