# Gsub("\\\\", "\\\\\\\\") seems unintuitive

**URL:** <https://www.ruby-forum.com/t/gsub-seems-unintuitive/129919>\
**Category:** Ruby\
**Created:** [February 22, 2008, 6:28pm UTC](https://www.ruby-forum.com/t/gsub-seems-unintuitive/129919 "2008-02-22T18:28:40Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [February 22, 2008, 6:28pm UTC](https://www.ruby-forum.com/t/gsub-seems-unintuitive/129919/1 "2008-02-22T18:28:40Z")

</div>

The following confusing behavior is noted in the pickaxe book (2nd ed)  
on page 75:

# I would expect two backslashes in the result

irb\> puts “\”.gsub("\","\\")  
\

# I would expect four backslashes in the result

irb\> puts “\”.gsub("\","\\\\")  
\

I can certainly work around it, but it seems unintuitive. Is there a  
reason why gsub behaves this way? Just curious…

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [February 22, 2008, 6:37pm UTC](https://www.ruby-forum.com/t/gsub-seems-unintuitive/129919/2 "2008-02-22T18:37:56Z")

</div>

John W. wrote:

> The following confusing behavior is noted in the pickaxe book (2nd ed)  
> on page 75:
> 
> # I would expect two backslashes in the result
> 
> irb\> puts “\”.gsub("\","\\")  
> \
> 
> # I would expect four backslashes in the result
> 
> irb\> puts “\”.gsub("\","\\\\")  
> \
> 
> I can certainly work around it, but it seems unintuitive. Is there a  
> reason why gsub behaves this way? Just curious…

It’s not a gsub thing, per se–it’s a string thing. Backslashes are used  
in strings to escape special characters. One such character is a " mark.  
If you want to write a " mark in the middle of a string, you have to  
escape it with a backslash:  
“They call me “Mellow Yellow” etc.”

If you didn’t, then the " would signify the end of the string!  
Similarly, in the example you listed, if you just did:  
“”  
then you end up with a string that ISN’T ended! Because you escaped the  
next ". So, if you want a literal backslash, you have to escape the  
backslash too: “\”

It just looks confusing because you are escaping the escape character 🙂

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [February 22, 2008, 6:41pm UTC](https://www.ruby-forum.com/t/gsub-seems-unintuitive/129919/3 "2008-02-22T18:41:05Z")

</div>

On Feb 22, 11:27 am, John W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I can certainly work around it, but it seems unintuitive. Is there a  
> reason why gsub behaves this way? Just curious…

puts “\”.gsub(“\”){“\\”}

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [February 22, 2008, 6:49pm UTC](https://www.ruby-forum.com/t/gsub-seems-unintuitive/129919/4 "2008-02-22T18:49:20Z")

</div>

On Feb 22, 2008, at 12:27 PM, John W. wrote:

> I can certainly work around it, but it seems unintuitive. Is there a  
> reason why gsub behaves this way? Just curious…

Notwithstanding the earlier responses…

Since the replacement string is evaluated ‘twice’, once as a ruby  
string literal and then again by gsub to look for group refrences like  
‘\1’, you need to provide two levels of escaping for a backslash.

\ is “\”  
so two of them is “\\”  
and you want gsub to see that so it need to have them escaped: “\\\  
\”

Whew! Yeah, it’s unfortunate, but backslash is doing double-duty  
here: introducing a group reference to the regular expression and  
escaping characters in a string literal (just like “\n”, but also  
itself).

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [February 22, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/gsub-seems-unintuitive/129919/5 "2008-02-22T18:54:37Z")

</div>

Just as a side note, this is typical in all real programming languages.  
C, C++, Java, Perl, sh, etc.  
I _believe_ it’s also true in python, lithp/scheme, & (o)caml, but for  
those I’ve either not used them, or not used them in so long I’m  
unsure.

Some languages, like vb{[6|script|.net](http://6%7Cscript%7C.net)}, use a doubled quote, but those  
aren’t really proper programing languages 😉

–Kyle

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [February 22, 2008, 7:01pm UTC](https://www.ruby-forum.com/t/gsub-seems-unintuitive/129919/6 "2008-02-22T19:01:45Z")

</div>

Thanks Rob, that’s exactly what I was missing – the second round of  
escaping is necessary to make escaped references to regex groups work.

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [December 17, 2008, 10:15pm UTC](https://www.ruby-forum.com/t/gsub-seems-unintuitive/129919/7 "2008-12-17T22:15:40Z")

</div>

I stumbled onto this thread trying to figure out a similar problem…

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [December 17, 2008, 10:19pm UTC](https://www.ruby-forum.com/t/gsub-seems-unintuitive/129919/8 "2008-12-17T22:19:52Z")

</div>

Joe P. wrote:

> I stumbled onto this thread trying to figure out a similar problem…

Dangit, I didn’t mean to post the above message 🙂

Okay, I need to read a site name (like “[joe.com](http://joe.com)”) and put a \ in front  
of the period (so it would be “joe.com”). I’m having trouble with  
this, I can’t seem to get only ONE \ in front of the period…

> > “[joe.com](http://joe.com)”.gsub(/./, “\.”)  
> > =\> “joe\.com”  
> > “[joe.com](http://joe.com)”.gsub(/./, “\\.”)  
> > =\> “joe\.com”

Anyone have a clue what to do? I’m probably missing something simple.

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [December 17, 2008, 10:29pm UTC](https://www.ruby-forum.com/t/gsub-seems-unintuitive/129919/9 "2008-12-17T22:29:32Z")

</div>

On Wed, Dec 17, 2008 at 2:12 PM, Joe P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> =\> “joe\.com”
> 
> > > “[joe.com](http://joe.com)”.gsub(/./, “\\.”)  
> > > =\> “joe\.com”
> 
> ## Anyone have a clue what to do? I’m probably missing something simple.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

That’s because you are in irb. You are getting what you want try this:

puts “[joe.com](http://joe.com)”.gsub(/./, “\\.”)

Actually it might be more readable as gsub(‘.’, ‘.’)

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [December 18, 2008, 4:20pm UTC](https://www.ruby-forum.com/t/gsub-seems-unintuitive/129919/10 "2008-12-18T16:20:47Z")

</div>

Okay, those last two explanations helped clear up my confusion. Thanks.

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [December 18, 2008, 12:06am UTC](https://www.ruby-forum.com/t/gsub-seems-unintuitive/129919/11 "2008-12-18T00:06:13Z")

</div>

On Wed, Dec 17, 2008 at 3:12 PM, Joe P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> =\> “joe\.com”
> 
> > > “[joe.com](http://joe.com)”.gsub(/./, “\\.”)  
> > > =\> “joe\.com”
> 
> Anyone have a clue what to do? I’m probably missing something simple.

You are looking at a string with escaping characters (i.e. what it  
would look like in double quotes). You can verify this by using #puts  
or “[joe.com](http://joe.com)”.gsub(‘.’, ‘.’).length.

hth,  
Todd
