# Puts "\\\\".gsub("\\\\", "\\\\\\\\")

**URL:** <https://www.ruby-forum.com/t/puts-gsub/135710>\
**Category:** Ruby\
**Created:** [April 24, 2008, 11:56am UTC](https://www.ruby-forum.com/t/puts-gsub/135710 "2008-04-24T11:56:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![martinus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f08c70/32.png) [@martinus](https://www.ruby-forum.com/u/martinus)\
**Post date:** [April 24, 2008, 11:56am UTC](https://www.ruby-forum.com/t/puts-gsub/135710/1 "2008-04-24T11:56:16Z")

</div>

Hello, I have a mini-ruby quiz. Guess what this line of code writes to  
the console, then try it for yourself:

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

Why is that so?

Martin

---

<div class="post-metadata">

**Author:** ![martinus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f08c70/32.png) [@martinus](https://www.ruby-forum.com/u/martinus)\
**Post date:** [April 24, 2008, 12:02pm UTC](https://www.ruby-forum.com/t/puts-gsub/135710/2 "2008-04-24T12:02:11Z")

</div>

From: martinus [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# Hello, I have a mini-ruby quiz. Guess what this line of code writes to

# the console, then try it for yourself:

# puts “\”.gsub(“\”, “\\”)

puts “\”.gsub(“\”, “\\”)  
  
#=\> nil

# Why is that so?

faq. escaping the escape in sub/gsub. search the archives.

maybe you want something like,

puts “\”.gsub(“\”){“\\”}  
\  
#=\> nil

ie, use block wc is a lot more handy.

kind regards -botp

---

<div class="post-metadata">

**Author:** ![martinus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f08c70/32.png) [@martinus](https://www.ruby-forum.com/u/martinus)\
**Post date:** [April 24, 2008, 2:38pm UTC](https://www.ruby-forum.com/t/puts-gsub/135710/3 "2008-04-24T14:38:15Z")

</div>

- martinus [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) (11:53) schrieb:

> Hello, I have a mini-ruby quiz. Guess what this line of code writes to  
> the console, then try it for yourself:
> 
> puts “\”.gsub(“\”, “\\”)
> 
> Why is that so?

Well, it’s an faq. In short: The backslash is special in strings _and_  
in the replacement text. So for every literal backslash you need four  
backslashes, which is quite unreadable. If you don’t need \1 and Co you  
better use the block form: gsub(“\”) { “\\” }.

mfg, simon … hth

---

<div class="post-metadata">

**Author:** ![martinus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f08c70/32.png) [@martinus](https://www.ruby-forum.com/u/martinus)\
**Post date:** [April 24, 2008, 3:15pm UTC](https://www.ruby-forum.com/t/puts-gsub/135710/4 "2008-04-24T15:15:57Z")

</div>

On Thu, Apr 24, 2008 at 8:35 AM, Simon K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Well, it’s an faq. In short: The backslash is special in strings _and_  
> in the replacement text. So for every literal backslash you need four  
> backslashes, which is quite unreadable. If you don’t need \1 and Co you  
> better use the block form: gsub(“\”) { “\\” }.

Another thing which trips up newbies, and sometimes not so newbies, is  
the difference between the contents of a sring, and the ‘inspect’  
presentation of a string, particularly when the string contains  
escapes:

irb(main):001:0\> puts “\”  
  
=\> nil  
irb(main):002:0\> p “\”  
“\”  
=\> nil

The point is that the literal string “\” only contains one character.  
The puts method shows you the contents of the string, while p (which  
is practically equivalent to puts string.inspect produces a literal  
representation.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![martinus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f08c70/32.png) [@martinus](https://www.ruby-forum.com/u/martinus)\
**Post date:** [April 25, 2008, 5:15am UTC](https://www.ruby-forum.com/t/puts-gsub/135710/5 "2008-04-25T05:15:50Z")

</div>

martinus [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello, I have a mini-ruby quiz. Guess what this line of code writes to  
> the console, then try it for yourself:
> 
> puts “\”.gsub(“\”, “\\”)

We were just up and down this road (and I gave a workaround):

[http://groups.google.com/group/comp.lang.ruby/msg/d370b684485c8838](http://groups.google.com/group/comp.lang.ruby/msg/d370b684485c8838)

m.
