T
Tyler
Hi all,
I'm trying to parse escaped unicode characters. The basic goal is to
read the string '\u00F3' (or "\\u00F3") as 'ó'. I have a workaround
below that uses eval (code below), but I'd be grateful if anyone had a
less dangerous solution or suggestion. In python, you can 'import
codecs' and use string.decode("unicode-escape"), is something similar
possible in Ruby?
Thanks!
Tyler
File.open("test.txt", 'w') {|file| file.puts "Asociaci\\u00F3n Alumni
\nF\\u00FAtbol"}
File.open "test.txt", 'r' do |file|
file.each do |line|
puts eval("%Q{#{line}}")
# puts line
end
end
# => Asociación Alumni
# => Fútbol
#
# If 'puts line' is used instead, this is the output:
# => Asociaci\u00F3n Alumni
# => F\u00FAtbol
#
# Is there a (prettier & safer) way to do this without using eval?
I'm trying to parse escaped unicode characters. The basic goal is to
read the string '\u00F3' (or "\\u00F3") as 'ó'. I have a workaround
below that uses eval (code below), but I'd be grateful if anyone had a
less dangerous solution or suggestion. In python, you can 'import
codecs' and use string.decode("unicode-escape"), is something similar
possible in Ruby?
Thanks!
Tyler
File.open("test.txt", 'w') {|file| file.puts "Asociaci\\u00F3n Alumni
\nF\\u00FAtbol"}
File.open "test.txt", 'r' do |file|
file.each do |line|
puts eval("%Q{#{line}}")
# puts line
end
end
# => Asociación Alumni
# => Fútbol
#
# If 'puts line' is used instead, this is the output:
# => Asociaci\u00F3n Alumni
# => F\u00FAtbol
#
# Is there a (prettier & safer) way to do this without using eval?