This example will show you how to escape and un-escape a value to be encoded in a URI and within HTML.
Require 'cgi' # Escapename = "Ruby? " Value = "Yes" URL = "Http://example.com /? "+ CGI. Escape (name) + '=' + CGI. Escape (value) +" & Var = t" # URL: http: // Example.com /? Ruby % 3f = Yes & Var = T Html = % (<a href = "# {CGI. escapehtml (URL)}"> example </a> ) # HTML: <A href = "http://example.com /? Ruby % 3f = Yes & amp; Var = T "> example </a> # Unescapename_encoded = Html. Match (/HTTP :( [^ "] +)/) [0] # name_encoded: http: // Example.com /? Ruby % 3f = Yes & amp; Var = T Href = CGI. unescapehtml (name_encoded) # href: http: // Example.com /? Ruby % 3f = Yes & Var = T Query = href. Match (/\? (. *) $/) [1 ] # Query: Ruby % 3f = Yes & Var = Tpairs = Query. Split ('&' ) # Pairs :[ "Ruby % 3f = yes", "Var = t" ] Name, Value = Pairs [0]. Split ('='). Map {| v | CGI. Unescape (v)} # name, value :[ "Ruby? "," Yes "]