Ruby CGI Methods

The following is a list of methods of the CGI class:

No.Method Description
1CGI::new([ level="query"])
Create a CGI object. query can be the following values:
  • query:No HTML generation output

  • html3: HTML3.2

  • html4: HTML4.0 Strict

  • html4Tr: HTML4.0 Transitional

  • html4Fr: HTML4.0 Frameset

2CGI::escape( str)
Use URL encoding to escape the string.
3CGI::unescape( str)
Decode a string encoded by escape().
4CGI::escapeHTML( str)
Encode HTML special characters, including: & < >.
5CGI::unescapeHTML( str)
Decode HTML special characters, including: & < >.
6CGI::escapeElement( str[, element...])
Encode HTML special characters within the specified HTML element.
7CGI::unescapeElement( str, element[, element...])
Decode HTML special characters within the specified HTML element.
8CGI::parse( query)
Parse the query string and return a hash containing key=>value pairs.
9CGI::pretty( string[, leader=" "])
Returns neatly formatted HTML. If specifiedleaderit will be written to the beginning of each line.leaderThe default value is two spaces.
10CGI::rfc1123_date( time)
Format time according to RFC-1123 (e.g., Tue, 2 Jun 2008 00:00:00 GMT).

CGI Instance Methods

In the following example, we assign the CGI::new object to variable c. The method list is as follows:

No.Method Description
1c[ name]
Returns an array containing the corresponding field namenamevalue.
2c.checkbox( name[, value[, check=false]])
c.checkbox( options)

Returns an HTML string for defining a checkbox field. Tag attributes can be passed as a hash argument.
3c.checkbox_group( name, value...)
c.checkbox_group( options)

> Returns an HTML string for defining a checkbox group. Tag attributes can be passed as a hash argument.
4c.file_field( name[, size=20[, max]])
c.file_field( options)

Returns an HTML string for defining a file field.
5c.form([ method="post"[, url]]) { ...}
c.form( options)

Returns an HTML string for defining a form. If a code block is specified, it will be output as the form content. Tag attributes can be passed as a hash argument.
6c.cookies
Returns a CGI::Cookie object containing the key-value pairs from the cookie.
7c.header([ header])
Returns the CGI header information. If the header parameter is a hash, its key-value pairs are used to create the header information.
8c.hidden( name[, value])
c.hidden( options)

Returns an HTML string for defining a hidden field. Tag attributes can be passed as a hash argument.
9c.image_button( url[, name[, alt]])
c.image_button( options)

Returns an HTML string for defining an image button. Tag attributes can be passed as a hash argument.
10c.keys
Returns an array containing the field names of the form.
11c.key?( name)
c.has_key?( name)
c.include?( name)

Returns true if the form contains the specified field name.
12c.multipart_form([ url[, encode]]) { ...}
c.multipart_form( options) { ...}

Returns an HTML string for defining a multipart form. Tag attributes can be passed as a hash argument.
13c.out([ header]) { ...}
Generate HTML and output it. Use the output of the block to create the string generated for the page body.
14c.params
Returns a hash containing form field names and values.
15c.params= hash
Set the field name and value to use.
16c.password_field( name[, value[, size=40[, max]]])
c.password_field( options)

Returns an HTML string for defining a password field. Tag attributes can be passed as a hash argument.
17c.popup_menu( name, value...)
c.popup_menu( options)
c.scrolling_list( name, value...)
c.scrolling_list( options)

Returns an HTML string for defining a pop-up menu. Tag attributes can be passed as a hash argument.
18c.radio_button( name[, value[, checked=false]])
c.radio_button( options)

Returns an HTML string for defining a radio field. Tag attributes can be passed as a hash argument.
19c.radio_group( name, value...)
c.radio_group( options)

Returns an HTML string for defining a radio button group. Tag attributes can be passed as a hash argument.
20c.reset( name[, value])
c.reset( options)

Returns an HTML string for defining a reset button. Tag attributes can be passed as a hash argument.
21c.text_field( name[, value[, size=40[, max]]])
c.text_field( options)

Returns an HTML string for defining a text field. Tag attributes can be passed as a hash argument.
22c.textarea( name[, cols=70[, rows=10]]) { ...}
c.textarea( options) { ...}

Returns an HTML string for defining a textarea field. If a block is specified, the string output by the block will be used as the content of the textarea. Tag attributes can be passed as a hash argument.

HTML Generation Methods

You can use the corresponding HTML tag names in a CGI instance to create HTML tags, as shown in the following example:

Example

#!/usr/bin/ruby require "cgi" cgi = CGI.new("html4") cgi.out{ cgi.html{ cgi.head{ "\n"+cgi.title{"This Is a Test"} } + cgi.body{ "\n"+ cgi.form{"\n"+ cgi.hr + cgi.h1 { "A Form: " } + "\n"+ cgi.textarea("get_text") +"\n"+ cgi.br + cgi.submit } } } }

CGI Object Attributes

You can use the following attributes in a CGI instance:

AttributeReturn value
acceptAcceptable MIME types
accept_charsetAcceptable character sets
accept_encodingAcceptable encodings
accept_languageAcceptable languages
auth_typeAcceptable types
raw_cookieCookie data (raw string)
content_lengthContent length
content_typeContent type
FromClient e-mail address
gateway_interfaceCGI version
path_infoPath
path_translatedTranslated path
Query_stringQuery string
refererPreviously visited URL
remote_addrClient host address (IP)
remote_hostClient host name
remote_identClient name
remote_userAuthenticated user
request_methodRequest method (GET, POST, etc.)
script_nameParameter names
server_nameServer name
server_portServer port
server_protocolServer protocol
server_softwareServer software
user_agentUser agent
Other extensions