E
Eyal Oren
Hi,
I've searched through the newsgroup, found some related stuff (about
'string interpolation at will') but couldn't quite figure it out
completely
My question: I read a string from file, using 'readlines', containing
special characters, especially \n. These characters are not
substituted into their binary control characters, e.g. my strings act
as if created with 'test\abc' instead of "test\nabc". I would like
to convert these strings and have Ruby take account of the special
meaning of the escaped characters, so that I can print them out
nicely.
I see two ways:
- use gsub: str.gsub('\n',"\n"), but this solves only \n, so I need to
handle each possible escaped character manually
- use eval: eval('"' + str + '"'), but this is dangerous, for example
I had one string that ends with \, thereby breaking the eval code
since the slash escapes the closing quote.
Is there a better solution? I have the feeling that unpack would help,
but I simply can't understand the documentation of unpack. I found
some discussions on str.interpolate, but I don't care for
interpolation of Ruby code, I only want replacement of these special
characters.
(I had the same problem for unicode escapes \uxxxx, but I solved that
with some unpack code from the web)
I've searched through the newsgroup, found some related stuff (about
'string interpolation at will') but couldn't quite figure it out
completely
My question: I read a string from file, using 'readlines', containing
special characters, especially \n. These characters are not
substituted into their binary control characters, e.g. my strings act
as if created with 'test\abc' instead of "test\nabc". I would like
to convert these strings and have Ruby take account of the special
meaning of the escaped characters, so that I can print them out
nicely.
I see two ways:
- use gsub: str.gsub('\n',"\n"), but this solves only \n, so I need to
handle each possible escaped character manually
- use eval: eval('"' + str + '"'), but this is dangerous, for example
I had one string that ends with \, thereby breaking the eval code
since the slash escapes the closing quote.
Is there a better solution? I have the feeling that unpack would help,
but I simply can't understand the documentation of unpack. I found
some discussions on str.interpolate, but I don't care for
interpolation of Ruby code, I only want replacement of these special
characters.
(I had the same problem for unicode escapes \uxxxx, but I solved that
with some unpack code from the web)