Method: ERB::Util#url_encode

Defined in:
lib/erb.rb

#url_encode(s) ⇒ Object (private) Also known as: u

A utility method for encoding the String s as a URL.

require "erb"
include ERB::Util

puts url_encode("Programming Ruby:  The Pragmatic Programmer's Guide")

Generates

Programming%20Ruby%3A%20%20The%20Pragmatic%20Programmer%27s%20Guide


930
931
932
933
934
# File 'lib/erb.rb', line 930

def url_encode(s)
  s.to_s.dup.force_encoding("ASCII-8BIT").gsub(/[^a-zA-Z0-9_\-.]/n) {
    sprintf("%%%02X", $&.unpack("C")[0])
  }
end