Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
CGI::Util#unescape
unescape(string, encoding = @@accept_charset)
lib/cgi/util.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.4.1/lib/cgi/util.rb, line 27
def unescape(string, encoding = @@accept_charset)
str = string.tr('+', ' ')
str = str.b
str.gsub!(/((?:%[0-9a-fA-F]{2})+)/) do |m|
[m.delete('%')].pack('H*')
end
str.force_encoding(encoding)
str.valid_encoding? ? str : str.force_encoding(string.encoding)
end
URL-decode an application/x-www-form-urlencoded string with encoding(optional).
Example
string = CGI.unescape("%27Stop%21%27+said+Fred") # => "'Stop!' said Fred"