There is actually a drop in replacement.
s="<html>this and that</html>"
p = URI::Parser.new
p.escape(s)
=> "%3Chtml%3Ethis%20and%20that%3C/html%3E"
Docs: https://docs.w3cub.com/ruby~3/uri/rfc2396_parser
Found this through a comment under this article
https://docs.knapsackpro.com/2020/uri-escape-is-obsolete-percent-encoding-your-query-string
Also tested this against some other strings in my setup, this also seems to retain commas the same way URI.escape
does, in contrast to ERB::Util.url_encode
.
NOTE:
As this answer became so popular now, it’s probably worth to mention that you should not blindly change your code to use URI::Parser
unless you are certain your project doesn’t need a standards compliant encoder. As URI.escape
was actually deprecated for a reason. So before simply switching to URI::Parser
make sure you have read and understood https://stackoverflow.com/a/13059657/6376353