Project

General

Profile

Actions

Bug #8136

closed

gsub strange behavior when substituting "\\'"

Added by alexeymuranov (Alexey Muranov) about 11 years ago. Updated about 11 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
2.0.0
Backport:
[ruby-core:53602]

Description

=begin
s = "\'" # => "\'"
print s # '
"foo".gsub("f", s) # => "oooo"

Can anybody please explain to me why it works like this? Is this a bug?
=end

Updated by Eregon (Benoit Daloze) about 11 years ago

Seems like ' is interpreted as $' ($POSTMATCH) which is in this case 'oo'.

Updated by nobu (Nobuyoshi Nakada) about 11 years ago

  • Category set to doc
  • Status changed from Open to Closed

This is one of most frequently asked questions.
Though I can't remember a pointer to good explanation, someone would know it.

Actions #3

Updated by alexeymuranov (Alexey Muranov) about 11 years ago

Excuse me, nobu, i have not understood, how can i replace something with a backslash followed by a single quote?

Updated by nobu (Nobuyoshi Nakada) about 11 years ago

=begin
You need more escapes.

"foo".gsub("f", "\\'")
=end

Updated by alexeymuranov (Alexey Muranov) about 11 years ago

nobu (Nobuyoshi Nakada) wrote:

=begin
You need more escapes.

"foo".gsub("f", "\\'")
=end

Thank you, i have not read the documentation carefully. It is still a bit strange to me that a replacement string is not taken literally by default.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0