# Irb and single quoted strings

**URL:** <https://www.ruby-forum.com/t/irb-and-single-quoted-strings/159570>\
**Category:** Ruby\
**Created:** [February 5, 2009, 9:51pm UTC](https://www.ruby-forum.com/t/irb-and-single-quoted-strings/159570 "2009-02-05T21:51:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [February 5, 2009, 9:51pm UTC](https://www.ruby-forum.com/t/irb-and-single-quoted-strings/159570/1 "2009-02-05T21:51:20Z")

</div>

This is the problem:

irb(main):002:0\> x = ‘–rails --exclude something\_is\_not\_right  
/usr/lib,test/lib’  
=\> “–rails --exclude something\_is\_not\_right \/usr\/lib,test\/lib”

irb(main):004:0\> p x  
“–rails --exclude something\_is\_not\_right \/usr\/lib,test\/lib”

How do I get a literal string like /usr/lib,test/lib into a variable?  
Where are these extra ‘’ artifacts coming from?

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [February 5, 2009, 10:22pm UTC](https://www.ruby-forum.com/t/irb-and-single-quoted-strings/159570/2 "2009-02-05T22:22:17Z")

</div>

James B. wrote:

> variable? Where are these extra ‘’ artifacts coming from?  
> You have it right, it’s just irb that backwacks it in response.

irb(main):004:0\> x = ‘–rails -exclude something\_is\_not\_right  
/usr/lib,test/lib’  
=\> “–rails -exclude something\_is\_not\_right \/usr\/lib,test\/lib”  
irb(main):005:0\> puts x  
–rails -exclude something\_is\_not\_right /usr/lib,test/lib  
=\> nil  
irb(main):006:0\>

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [February 5, 2009, 10:18pm UTC](https://www.ruby-forum.com/t/irb-and-single-quoted-strings/159570/3 "2009-02-05T22:18:08Z")

</div>

Le 5 février 2009 à 21:50, James B. a écrit :

> How do I get a literal string like /usr/lib,test/lib into a variable?  
> Where are these extra ‘’ artifacts coming from?

It’s just the representation. IRB always shows strings double quoted,  
and, in double quotes, a lone \ has to be escaped.

A small example is perhaps more convincing :

22:10 fred@balvenie:~% irb

> > x = ‘/usr/lib’  
> > =\> “\/usr\/lib”
> 
> > x.length  
> > =\> 10
> 
> > ‘/usr/lib’ == “\/usr\/lib”  
> > =\> true

HTH,

Fred

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [February 5, 2009, 10:27pm UTC](https://www.ruby-forum.com/t/irb-and-single-quoted-strings/159570/4 "2009-02-05T22:27:35Z")

</div>

\ is an escape character for strings. For example, using

a = ‘Today is the day, I’m going to New Jersey’

is interpreted as

a = ‘Today is the day, I’ m going to New Jersey ’

This tosses an error because the interpreter assumes the apostrophe in  
I’m  
is the end of the string. The \ escapes that apostrophe so that it’s  
merely  
text in the string, not a Ruby operator. The above line would also error  
out  
because ‘m’, ‘going’, ‘to’, ‘new’, and ‘jersey’ are unknown methods, and  
then also because the last ’ was read as the beginning of another  
string,  
causing the string to run to the end of the file and the interpreter  
yelling  
at you because the string wasn’t terminated. (In other contexts it would  
run  
until the next occurrence of the ’ character, tossing other errors.)

Same thing happens with the backslash.

\ = Escape character  
\ = \ in a string

Example:

irb(main):001:0\> puts “c:\documents”  
c:documents  
=\> nil

Here the \ is merely an escape character and thus does nothing since  
there’s  
no special characters after it. On the other hand:

irb(main):002:0\> puts “c:\documents”  
c:\documents  
=\> nil

The \ becomes \ because it used the first backslash as the escape  
character  
for the second backslash.

I hope that helps.

- jayce

* * *

From: “James B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Thursday, February 05, 2009 12:50 PM  
Newsgroups: comp.lang.ruby  
To: “ruby-talk ML” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: irb and single quoted strings
