Lai Yu-Hsuan
Lai Yu-Hsuan

Reputation: 28141

Ruby: escape escape sequence?

There is a string:

str = "foo\nbar"

How can I escape it to:

'foo\nbar'

?

I noticed "#{str}" doesn't work.

Upvotes: 2

Views: 1322

Answers (2)

Andrew Marshall
Andrew Marshall

Reputation: 97004

Add another \:

"foo\\nbar" == 'foo\nbar'  #=> true

Single-quoted strings do not have interpolation, so #{str} does nothing special in them.

Upvotes: 2

John Douthat
John Douthat

Reputation: 41209

str.inspect Should do it for you

Upvotes: 3

Related Questions