On 2013-05-29, at 8:57 AM, Tadeusz Bochan <lists / ruby-forum.com> wrote:

> This workaround (avoiding eval) works, but is ever so slow !!
>=20
> # convert all \nnn to corresponding character
> data=3Dtext.gsub(/\\[0-7]{3,4}/) {|r| r.scanf("%c%o").last.chr}
>=20
> Can any one suggest something better please ?
>=20
> --=20
> Posted via http://www.ruby-forum.com/.

Watch out for the 3 or 4 digit thing there, how can you tell the =
difference between a value encoded as 3 digits which happens to be =
followed by a "real" octal digit from the un-encoded data? you either =
need an explicit terminator e.g. \0123; or \012; or to guarantee the =
length of the encoded representation. Additionally you need to make sure =
any literal \ in the input are encoded too so you don't accidentally =
un-encode sequences which were in your original data.

text.gsub(/\\([0-7]{3})/) { $1.to_i(8).chr }

might be faster.

Hope this helps,

Mike

--=20

Mike Stok <mike / stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.