Israel
Israel

Reputation: 3402

How to print raw string in ruby?

I want to print escaped or raw version of a string. For example: given this string:

"a,
b,
c,
d"

I want to get

"a,\nb,\nc,\nd".

Is it possible?

Upvotes: 4

Views: 8669

Answers (2)

John La Rooy
John La Rooy

Reputation: 304137

s = "a,
b,
c,
d"
s.dump
# => "\"a,\\nb,\\nc,\\nd\"" 
s.dump[1...-1]
# => "a,\\nb,\\nc,\\nd" 

Upvotes: 9

Gagan Gami
Gagan Gami

Reputation: 10251

string = 'a,
b,
c,
d'

> p string.inspect
#=> "\"a,\\nb,\\nc,\\nd\""
# "*** expected output ***"
> p string.inspect.delete('\"')
#=> "a,\\nb,\\nc,\\nd"

Demo

Upvotes: 6

Related Questions