
The closest to the spec seems to be the Addressable gem: require 'addressable/uri'Īddressable::URI.escape ' Blog & Your Blog' Actually the most common use-case is escaping form data, such as while sending an application/x-is not much of improvement (again it's just a simple gsub, which is, IMO, even a worse option than URI.escape): WEBrick::HTTPUtils.escape ' Blog & Your Blog' # => "http%3A%2F%%2Ffoo%3Fbar%3Dat%23anchor%26title%3DMy+Blog+%26+Your+Blog"ĬGI::escape should be used for query parameters only, but the results will be, again, against the spec. Some proposed to use CGI::Escape for query parameters, because you couldn't escape an entire URI: CGI::escape ' Blog & Your Blog'

Unfortunately there is not a single word about it in the docs, the only way to know about it is to check the source, or run the script with warnings in verbose level ( -wW2) (or use some google-fu). This willīe removed or change behavior drastically.Īs I said above, current URI.encode is wrong on spec level. So current URI.encode is considered harmful and deprecated. Split a URI to components, then escape each components, and finally

Moreover current URI.encode is simple gsub. The problem with URI.escape was that it could not handle the RFC-3896 spec. It is a more specific use case.įor that, the Ruby community used URI.escape for years. Now, let's say that you need to escape a URI in your app. URI.escape was supposed to encode a string (URL) into, so called, " Percent-encoding".ĬGI::escape is coming from the CGI spec, which describes how data should be encoded/decode between web server and application. What's the difference between an axe and a sword and which one I should use? Well it depends on what you need to do.
