From: 7stud -- Date: 2009-02-23T15:23:10+09:00 Subject: Re: Improving hexadecimal escaping performance I単aki Baz Castillo wrote: > I don't like > too > much its approach using "sprintf". Is there other way more ellegant? > (performance is the mos important requeriment anyway). > pickaxe2, p. 23: ------ Another output method we use a lot is printf.... ------ pickaxe2, p. 526: -------- printf Equivalent to io.write sprintf(...) -------- The Ruby Way (2nd), p. 72: ---------- 2.9 Formatting a String This is done in Ruby as it is in C, with the sprintf method. --------- >Is there other way more ellegant? def hex_escape(str) str.gsub(/[^a-zA-Z0-9_\-.]/n) do |match| "%%%02X" % match[0] end end s = "?<>辿" puts hex_escape(s) --output:-- %3F%3C%3E%C3%A9 -- Posted via http://www.ruby-forum.com/.