Ruby 4.1 リファレンスマニュアル

singleton method CGI.escapeURIComponent

escapeURIComponent(string) -> StringRuby 3.2 から[permalink][rdoc][edit] [added by cgi/util]

与えられた文字列を [RFC3986] に従って URL エンコードした文字列を新しく作成し返します。

CGI.escape と異なり、空白文字を + ではなく %20 に変換します。 JavaScript の encodeURIComponent 関数 と同じ結果になります。

[PARAM] string:
URL エンコードしたい文字列を指定します。
require "cgi"

p CGI.escapeURIComponent("'Stop!' said Fred") #=> "%27Stop%21%27%20said%20Fred"

# CGI.escape は空白を + にするが、escapeURIComponent は %20 にする
p CGI.escape("a b")             #=> "a+b"
p CGI.escapeURIComponent("a b") #=> "a%20b"

[SEE_ALSO] CGI.escape, CGI.unescapeURIComponent