# How to represent a single backslash?

**URL:** <https://www.ruby-forum.com/t/how-to-represent-a-single-backslash/182217>\
**Category:** Ruby\
**Created:** [January 25, 2010, 9:55am UTC](https://www.ruby-forum.com/t/how-to-represent-a-single-backslash/182217 "2010-01-25T09:55:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lalawawa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e480ec/32.png) [@lalawawa](https://www.ruby-forum.com/u/lalawawa)\
**Post date:** [January 25, 2010, 9:55am UTC](https://www.ruby-forum.com/t/how-to-represent-a-single-backslash/182217/1 "2010-01-25T09:55:49Z")

</div>

How do a put a single backslash into a string in Ruby?

irb(main):006:0\> ‘’  
irb(main):007:0’ ’  
=\> “’\n”  
irb(main):008:0\> ‘\’  
=\> “\”  
irb(main):009:0\>

in 6, the backslash escapes the ending ', which is not what I wanted.  
In 8, the double backslash becomes two backslashes, which is not what I  
wanted either.  
I get exactly the same problem with double quotes.

---

<div class="post-metadata">

**Author:** ![lalawawa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e480ec/32.png) [@lalawawa](https://www.ruby-forum.com/u/lalawawa)\
**Post date:** [January 25, 2010, 1:34pm UTC](https://www.ruby-forum.com/t/how-to-represent-a-single-backslash/182217/2 "2010-01-25T13:34:10Z")

</div>

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

There is a difference between what is written in your code and what  
the output will be.  
“\” is not the same as “”, which is why irb prints the string  
constant as you entered it. But if you print it, it will output what  
you expected, i.e. a backslash.

---

<div class="post-metadata">

**Author:** ![lalawawa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e480ec/32.png) [@lalawawa](https://www.ruby-forum.com/u/lalawawa)\
**Post date:** [January 25, 2010, 1:34pm UTC](https://www.ruby-forum.com/t/how-to-represent-a-single-backslash/182217/3 "2010-01-25T13:34:10Z")

</div>

On Mon, Jan 25, 2010 at 9:55 AM, lalawawa [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> the double backslash becomes two backslashes, which is not what I wanted  
> either.  
> I get exactly the same problem with double quotes.

It’s not 2 backslashes, just one. Irb shows you two because it’s the  
representation of the inspected string:

irb(main):003:0\> ‘\’  
=\> “\”  
irb(main):004:0\> ‘\’.size  
=\> 1  
irb(main):005:0\> puts ‘\’  
  
=\> nil

Jesus.

---

<div class="post-metadata">

**Author:** ![lalawawa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e480ec/32.png) [@lalawawa](https://www.ruby-forum.com/u/lalawawa)\
**Post date:** [January 25, 2010, 1:46pm UTC](https://www.ruby-forum.com/t/how-to-represent-a-single-backslash/182217/4 "2010-01-25T13:46:28Z")

</div>

2010/1/25 Xavier Noëlle [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> constant as you entered it. But if you print it, it will output what  
> you expected, i.e. a backslash.

Right. Additional explanation: the reason for this is that IRB by  
default uses #inspect - the same method that #p uses for printing:

irb(main):007:0\> s = “a”  
=\> “a”  
irb(main):008:0\> puts s  
a  
=\> nil  
irb(main):009:0\> p s  
“a”  
=\> “a”  
irb(main):010:0\> puts s.inspect  
“a”  
=\> nil  
irb(main):011:0\> s  
=\> “a”  
irb(main):012:0\>

Kind regards

robert
