Ruby URI escape
mouse 460 · person cloud · link
Last update
2024-08-23
2024
08-23
«encode/decode URI»
  • URI.escape is deprecated and not fully conformant to RFC specs.

  • In ruby >=3.2 you can use CGI.escapeURIComponent:

1
2
3
4
require 'cgi'
"https://a.com/b/" +
  "#{ CGI.escapeURIComponent path_component }" + 
  "?#{CGI.escapeURIComponent my_key}=#{CGI.escapeURIComponent my_value}"
  • In ruby <3.2 you can use obj.to_param and Hash#to_query, it calls Object#to_query implemented as:
1
def to_query(key) = "#{CGI.escape(key.to_param)}=#{CGI.escape(to_param.to_s)}"

Source: bibwild, stackoverflow