123456789_123456789_123456789_123456789_123456789_

Module: ERB::Util

Relationships & Source Files
Defined in: lib/erb.rb

Overview

A utility module for conversion routines, often handy in HTML generation.

Class Method Summary

Class Method Details

.h(s) (mod_func)

Alias for #html_escape.

[ GitHub ]

  
# File 'lib/erb.rb', line 1005

alias h html_escape

.html_escape(s) (mod_func) Also known as: #h

A utility method for escaping HTML tag characters in s.

require "erb"
include ERB::Util

puts html_escape("is a > 0 & a < 10?")

Generates

is a &gt; 0 &amp; a &lt; 10?
[ GitHub ]

  
# File 'lib/erb.rb', line 1002

def html_escape(s)
  CGI.escapeHTML(s.to_s)
end

.u(s) (mod_func)

Alias for #url_encode.

[ GitHub ]

  
# File 'lib/erb.rb', line 1026

alias u url_encode

.url_encode(s) (mod_func) 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
[ GitHub ]

  
# File 'lib/erb.rb', line 1021

def url_encode(s)
  s.to_s.b.gsub(/[^a-zA-Z0-9_\-.~]/n) { |m|
    sprintf("%%%02X", m.unpack1("C"))
  }
end