Module: ERB::Util

Defined in:
lib/erb.rb

Overview

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

Class Method Summary collapse

Class Method Details

.hObject

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?

935
936
937
# File 'lib/erb.rb', line 935

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

.html_escape(s) ⇒ Object

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?

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

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

.uObject

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

956
957
958
959
960
# File 'lib/erb.rb', line 956

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

.url_encode(s) ⇒ Object

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

951
952
953
954
955
# File 'lib/erb.rb', line 951

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