What is the coolest way of unescaping strings?
I.e. I have a string which contains things like
\n
\t
\
"
""
etc each of which are two characters which should be converted to a
single character.
I have a feeling there must be a one line way of doing this intead of
the multi line subroutine I have at the moment.
Michael Chapman wrote:
What is the coolest way of unescaping strings?
I.e. I have a string which contains things like
\n
\t
\
"
“”
etc each of which are two characters which should be converted to a
single character.
I have a feeling there must be a one line way of doing this intead of
the multi line subroutine I have at the moment.
One line, but kind of “evil”:
eval “"#{s}"”
Evil, because it allows
s = ‘#{do_anything(); “I didn’t do it.”}’
One line too, but less evil:
astring.gsub!(/\[nt\"]/) { |x| eval “"#{x}"” }
=>
astring = ‘[\nA\tB\\C"D]’
puts astring, astring.inspect
astring.gsub!(/\[nt\"]/) { |x| eval “"#{x}"” }
puts astring, astring.inspect
==>
[\nA\tB\C"D]
“[\nA\tB\\C\"D]”
[
A B\C"D]
“[\nA\tB\C"D]”
···
On Mon, Aug 19, 2002 at 03:35:42PM +0900, Joel VanderWerf wrote:
One line, but kind of “evil”:
eval “"#{s}"”
–
_ _
__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_
_ \ / ` | ’ \
) | (| | |__ \ | | | | | (| | | | |
.__/ _,|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com
‘Ooohh… “FreeBSD is faster over loopback, when compared to Linux
over the wire”. Film at 11.’
– Linus Torvalds