# Using a \\ in gsub

**URL:** <https://www.ruby-forum.com/t/using-a-in-gsub/98545>\
**Category:** Ruby\
**Created:** [July 15, 2006, 10:54pm UTC](https://www.ruby-forum.com/t/using-a-in-gsub/98545 "2006-07-15T22:54:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![metrix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@metrix](https://www.ruby-forum.com/u/metrix)\
**Post date:** [July 15, 2006, 10:54pm UTC](https://www.ruby-forum.com/t/using-a-in-gsub/98545/1 "2006-07-15T22:54:11Z")

</div>

In IRB if I type the following:  
“te/st”.gsub(’/’,’’)  
into irb, IRB screws up.  
but if I type  
File.expand\_path($0).gsub(’/’,’\’)  
I get te\st. what gives?? how do I get ruby to come up with te\st?

what I am trying to do is this:  
File.expand\_path($0).gsub(’/’,’’) because with ruby in windows it uses  
a / instead of a \ for path names which is screwing my pathnames up…  
any suggestions???

---

<div class="post-metadata">

**Author:** ![metrix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@metrix](https://www.ruby-forum.com/u/metrix)\
**Post date:** [July 15, 2006, 10:59pm UTC](https://www.ruby-forum.com/t/using-a-in-gsub/98545/2 "2006-07-15T22:59:40Z")

</div>

On 7/15/06, brandon coleman [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> any suggestions???  
> File.expand\_path($0).gsub(%r{/}) { “\” }

-austin

---

<div class="post-metadata">

**Author:** ![metrix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@metrix](https://www.ruby-forum.com/u/metrix)\
**Post date:** [July 15, 2006, 11:05pm UTC](https://www.ruby-forum.com/t/using-a-in-gsub/98545/3 "2006-07-15T23:05:21Z")

</div>

Austin Z. wrote:

> On 7/15/06, brandon coleman [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > any suggestions???  
> > File.expand\_path($0).gsub(%r{/}) { “\” }
> 
> -austin

that would give me two c:\ruby\ whatever and if I remove a \ it still  
gives me an error…

---

<div class="post-metadata">

**Author:** ![metrix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@metrix](https://www.ruby-forum.com/u/metrix)\
**Post date:** [July 16, 2006, 12:17am UTC](https://www.ruby-forum.com/t/using-a-in-gsub/98545/4 "2006-07-16T00:17:19Z")

</div>

On Saturday 15 July 2006 23:05, brandon coleman wrote:

> Austin Z. wrote:
> 
> > On 7/15/06, brandon coleman [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > any suggestions???
> > 
> > File.expand\_path($0).gsub(%r{/}) { “\” }
> > 
> > -austin
> 
> that would give me two c:\ruby\ whatever and if I remove a \ it  
> still gives me an error…

Are you testing this in irb? You should know  
that \ is used as escape character inside of Ruby string  
literals. Thus if you want an actual backslash in the string,  
you have to escape it with another backslash.  
irb prints a ruby string literal to stdout, thus you see a  
double backslash for every actual backslash in the string.

Try:

> puts File.expand\_path($0).gsub(%r{/}, “\”)  
> to see the “real” contents of the resulting string.

BTW: The literal "" gives an error because the backslash  
before the second quote tells Ruby to include the quote  
in the string instead of interpreting it as string delimiter.
