# Gsub can't add a single backslash?

**URL:** <https://rubytalk.org/t/gsub-cant-add-a-single-backslash/2048>\
**Category:** ruby-talk\
**Created:** [28 September 2002 20:58 UTC](https://rubytalk.org/t/gsub-cant-add-a-single-backslash/2048 "2002-09-28T20:58:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philip\_Mak1](https://avatars.discourse-cdn.com/v4/letter/p/3ab097/32.png) [@Philip\_Mak1](https://rubytalk.org/u/Philip_Mak1)\
**Post date:** [28 September 2002 20:58 UTC](https://rubytalk.org/t/gsub-cant-add-a-single-backslash/2048/1 "2002-09-28T20:58:31Z")

</div>

I want to escape a string such that all occurances of ‘&’ become ‘&’.

I can’t seem to do it with gsub.

irb(main):014:0\> ‘&’.gsub(/&/, “&”)  
"&“  
irb(main):015:0\> ‘&’.gsub(/&/, “\&”)  
”&“  
irb(main):016:0\> ‘&’.gsub(/&/, “\&”)  
”&“  
irb(main):017:0\> ‘&’.gsub(/&/, “\\&”)  
”\&"

Have I stumbled upon a bug? It refuses to add a single backslash;  
always zero or two.

---

<div class="post-metadata">

**Author:** ![Philip\_Mak1](https://avatars.discourse-cdn.com/v4/letter/p/3ab097/32.png) [@Philip\_Mak1](https://rubytalk.org/u/Philip_Mak1)\
**Post date:** [28 September 2002 21:10 UTC](https://rubytalk.org/t/gsub-cant-add-a-single-backslash/2048/2 "2002-09-28T21:10:11Z")

</div>

Never mind, I was just reading it wrong. “\&” is actually ‘&’; it’s  
just the way irb displays strings.

> **···**
>
> On Sat, Sep 28, 2002 at 04:54:55PM -0400, Philip Mak wrote:
> 
> > irb(main):016:0\> ‘&’.gsub(/&/, “\&”)  
> > “&”  
> > irb(main):017:0\> ‘&’.gsub(/&/, “\\&”)  
> > “\&”
> > 
> > Have I stumbled upon a bug? It refuses to add a single backslash;  
> > always zero or two.
