Encode URL in Ruby

Syntax

ERB::Util.url_encode(uri)

CGI.escape(uri)

Example 1:

ERB::Util.url_encode("foo/bar? baz&")

Result:

foo%2Fbar%3F%20baz%26

Example 2:

CGI.escape("foo/bar? baz&")

Result:

foo%2Fbar%3F+baz%26


Decode URL in Ruby

Syntax

CGI.unescape(encoded_uri)

Example:

CGI.unescape("foo%2Fbar%3F+baz%26")

Result:

foo/bar? baz&