Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I unescape c-style escape sequences from ruby?

Tags:

ruby

In ruby, how do I decode c-style escape sequences? e.g. '\n' to a newline, '\t' to a tab?

like image 864
Simon Avatar asked Nov 24 '10 10:11

Simon


1 Answers

Shorter, even more hacky and fairly dangerous, due to eval:

eval "\"#{string}\""
A simple example:
> a = '1\t2\n3'
> puts a
1\t2\n3
> puts eval "\"#{a}\""
1       2
3

like image 97
GreyCat Avatar answered Nov 08 '22 22:11

GreyCat