Sometimes, I find the need to decode url encoded strings. I was
surprised not to find anything for that in erb.rb, so I added it to my
code. It''s useful enough that I''m sharing.
It would be nice if ruby had \x for matching a hex digit, instead of
the ugly [A-F\d] I''m using.
def url_decode(s)
s.to_s.gsub(/%([A-F\d]{2})/in) { [$1.hex].pack(''C'') }
end
--
Justin Dossey
jbd-ARXITx83Qt49fbuuu1rVSg@public.gmane.org wrote:> def url_decode(s) > s.to_s.gsub(/%([A-F\d]{2})/in) { [$1.hex].pack(''C'') } > endSee CGI.unescape
> It would be nice if ruby had \x for matching a hex digit, instead of > the ugly [A-F\d] I''m using. > > def url_decode(s) > s.to_s.gsub(/%([A-F\d]{2})/in) { [$1.hex].pack(''C'') } > end[:xdigit:] should match a hex digit (POSIX regex) Michale