Project

General

Profile

Bug #8136

gsub strange behavior when substituting "\\'"

Added by Alexey Muranov almost 3 years ago. Updated almost 3 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
ruby -v:
2.0.0
Backport:
2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN
[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

History

#1 [ruby-core:53603] Updated by Benoit Daloze almost 3 years ago

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

#2 [ruby-core:53847] Updated by Nobuyoshi Nakada almost 3 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.

#3 Updated by Alexey Muranov almost 3 years ago

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

#4 [ruby-core:53874] Updated by Nobuyoshi Nakada almost 3 years ago

=begin
You need more escapes.

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

#5 [ruby-core:53878] Updated by Alexey Muranov almost 3 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.

Also available in: Atom PDF