Parsing string to add to URL-encoded URL

In 2019, URI.encode is obsolete and should not be used.


require 'uri'

URI.encode("Hello there world")
#=> "Hello%20there%20world"
URI.encode("hello there: world, how are you")
#=> "hello%20there:%20world,%20how%20are%20you"

URI.decode("Hello%20there%20world")
#=> "Hello there world"

While the current answer says to utilize URI.encode that has been deprecated and obsolete since Ruby 1.9.2. It is better to utilize CGI.escape or ERB::Util.url_encode.