Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Latest commit

 

History

History
29 lines (21 loc) · 640 Bytes

File metadata and controls

29 lines (21 loc) · 640 Bytes

In Ruby, we use the CGI class to encode a URL. We first need to require the CGI library using require "cgi". This is similar to using require_once to include a class in PHP.

To get the equivalent result of PHP's urlencode function, we'll use the CGI.escape class method.

{{code:php $result = urlencode("Hello there!"); var_export($result); // => 'Hello+%3Cb%3Ethere%21%3C%2Fb%3E' }}

{{code:ruby require 'cgi'

p CGI.escape("Hello <b>there!</b>")
# => "Hello+%3Cb%3Ethere%21%3C%2Fb%3E"

}}

{{related: url/urldecode strings/htmlentities url/rawurlencode url/rawurldecode }}