instance method
CGI::Escape#escapeURIComponent
(::string) -> ::String
escapeURIComponent(string)
lib/cgi/escape.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.0.0/lib/cgi/escape.rb, line 55
def escapeURIComponent(string)
encoding = string.encoding
buffer = string.b
buffer.gsub!(/([^a-zA-Z0-9_.\-~]+)/) do |m|
'%' + m.unpack('H2' * m.bytesize).join('%').upcase
end
buffer.force_encoding(encoding)
end
URL-encode a string following RFC 3986 Space characters (" ") are encoded with ("%20")
Example
url_encoded_string = CGI.escapeURIComponent("'Stop!' said Fred") # => "%27Stop%21%27%20said%20Fred"