O'Reilly logo

Ruby in a Nutshell by Yukihiro Matsumoto

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

CGI — CGI support class

Synopsis

CGI provides useful features to implement Common Gateway Interface (CGI) programs, such as retrieving CGI data from server, manipulating cookies, and generating the HTTP header and the HTML body.

Example

require 'cgi'

     cgi = CGI::new("html3")

     input, = cgi["input"]
     if input
       input = CGI::unescape(input)
     end
     p input

     begin
       value = Thread::new{
         $SAFE=4
         eval input
       }.value.inspect
     rescue SecurityError
       value = "Sorry, you can't do this"
     end

     cgi.out {
       cgi.html{
         cgi.head{cgi.title{"Walter's Web Arithmetic Page"}} +
         cgi.body{
           cgi.form("post", "/cgi-bin/arith.rb") {
             "input your favorite expression: " +
             cgi.text_field("input", input) +
             cgi.br +
             "the result of you input: " +
             CGI::escapeHTML(value) +
             cgi.br +
             cgi.submit
           }
         }
       }
     }

Required Library

require 'cgi'

Class Methods

CGI::new([level="query"])

Creates a CGI object. level may be one of the following options. If one of the HTML levels is specified, the following methods are defined for generating output conforming to that level:

query

No HTML output generated

html3

HTML3.2

html4

HTML4.0 Strict

html4Tr

HTML4.0 Transitional

html4Fr

HTML4.0 Frameset

CGI::escape(str)

Escapes an unsafe string using URL-encoding.

CGI::unescape(str)

Expands a string that has been escaped using URL-encoding.

CGI::escapeHTML(str)

Escapes HTML special characters, including: & < >.

CGI::unescapeHTML(str)

Expands escaped HTML special characters, including: & < >.

CGI::escapeElement(str[, element...])

Escapes HTML special characters in the specified HTML elements. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required