Skip to content

Commit 6216693

Browse files
[DOC] Revise some examples (#81)
1 parent 5a660cd commit 6216693

File tree

1 file changed

+58
-48
lines changed

1 file changed

+58
-48
lines changed

lib/cgi.rb

Lines changed: 58 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@
4242
#
4343
# Read on for more details. Examples are provided at the bottom.
4444
#
45+
# == About the Examples
46+
#
47+
# Examples on this page assume that \CGI has been required:
48+
#
49+
# require 'cgi'
50+
#
51+
# Unless otherwise stated, examples also assume that environment variable 'REQUEST_METHOD' exists
52+
# (which prevents CGI.new from entering its online mode):
53+
#
54+
# ENV.include?('REQUEST_METHOD') # => true
55+
#
4556
# == Queries
4657
#
4758
# The CGI class dynamically mixes in parameter and cookie-parsing
@@ -148,59 +159,62 @@
148159
# Escape and unescape methods are defined in cgi/escape.rb.
149160
# And when include, you can use utility methods like a function.
150161
#
151-
# == Examples of use
162+
# == Examples of Use
152163
#
153-
# === Get form values
164+
# === Form Values
154165
#
155-
# require "cgi"
156-
# cgi = CGI.new
157-
# value = cgi['field_name'] # <== value string for 'field_name'
158-
# # if not 'field_name' included, then return "".
159-
# fields = cgi.keys # <== array of field names
160-
#
161-
# # returns true if form has 'field_name'
162-
# cgi.has_key?('field_name')
163-
# cgi.has_key?('field_name')
164-
# cgi.include?('field_name')
166+
# ==== Get Form Values
165167
#
166-
# CAUTION! <code>cgi['field_name']</code> returned an Array with the old
167-
# cgi.rb(included in Ruby 1.6)
168+
# You can use method +cgi.params+ to retrieve form values
169+
# in a {Hash}[https://docs.ruby-lang.org/en/3.4/Hash.html]:
168170
#
169-
# === Get form values as hash
170-
#
171-
# require "cgi"
171+
# ENV.update(
172+
# 'REQUEST_METHOD' => 'GET',
173+
# 'QUERY_STRING' => 'a=111&&b=222&c&d='
174+
# )
172175
# cgi = CGI.new
173-
# params = cgi.params
174-
#
175-
# cgi.params is a hash.
176-
#
177-
# cgi.params['new_field_name'] = ["value"] # add new param
178-
# cgi.params['field_name'] = ["new_value"] # change value
179-
# cgi.params.delete('field_name') # delete param
180-
# cgi.params.clear # delete all params
181-
#
182-
#
183-
# === Save form values to file
184-
#
185-
# require "pstore"
186-
# db = PStore.new("query.db")
187-
# db.transaction do
188-
# db["params"] = cgi.params
176+
# cgi.params.class # => Hash
177+
# cgi.params # => {"a" => ["111"], "b" => ["222"], "c" => [], "d" => [""]}
178+
# cgi.params.keys # => ["a", "b", "c", "d"]
179+
# cgi.params['a'] # => ["111"] # Returns an array.
180+
# cgi.params['d'] # => [""] # Returns empty string in array if no value.
181+
# cgi.params['x'] # => [] # Returns empty array if no key.
182+
#
183+
# A \CGI instance has these convenience methods:
184+
#
185+
# # Convenience method for cgi.params.keys.
186+
# cgi.keys # => ["a", "b", "c", "d"]
187+
# # Convenience method for cgi.params[key].first.
188+
# cgi['a'] # => "111" # Returns string, not array.
189+
# cgi['d'] # => "" # Returns empty string if no value.
190+
# cgi['x'] # => "" # Returns empty string if no key.
191+
# # Convenience method for cgi.params.include?.
192+
# cgi.include?('a') # => true
193+
# cgi.include?('x') # => false
194+
#
195+
# ==== Save and Restore Form Values
196+
#
197+
# This example uses {Pstore}[https://docs.ruby-lang.org/en/3.4/PStore.html]
198+
# to store and retrieve form values:
199+
#
200+
# ENV.update(
201+
# 'REQUEST_METHOD' => 'GET',
202+
# 'QUERY_STRING' => 'a=111&&b=222&c&d='
203+
# )
204+
# cgi = CGI.new
205+
# require 'pstore'
206+
# store = PStore.new('params.store')
207+
# store.transaction do
208+
# store['params'] = cgi.params
189209
# end
190-
#
191-
#
192-
# === Restore form values from file
193-
#
194-
# require "pstore"
195-
# db = PStore.new("query.db")
196-
# db.transaction do
197-
# cgi.params = db["params"]
210+
# cgi.params.clear # Oops! Lost my params!
211+
# store.transaction do
212+
# cgi.params = store['params']
198213
# end
214+
# cgi.params # => {"a" => ["111"], "b" => ["222"], "c" => [], "d" => [""]}
199215
#
216+
# ==== Get multipart form values
200217
#
201-
# === Get multipart form values
202-
#
203-
# require "cgi"
204218
# cgi = CGI.new
205219
# value = cgi['field_name'] # <== value string for 'field_name'
206220
# value.read # <== body of value
@@ -212,7 +226,6 @@
212226
#
213227
# === Get cookie values
214228
#
215-
# require "cgi"
216229
# cgi = CGI.new
217230
# values = cgi.cookies['name'] # <== array of 'name'
218231
# # if not 'name' included, then return [].
@@ -222,7 +235,6 @@
222235
#
223236
# === Get cookie objects
224237
#
225-
# require "cgi"
226238
# cgi = CGI.new
227239
# for name, cookie in cgi.cookies
228240
# cookie.expires = Time.now + 30
@@ -231,14 +243,12 @@
231243
#
232244
# cgi.cookies # { "name1" => cookie1, "name2" => cookie2, ... }
233245
#
234-
# require "cgi"
235246
# cgi = CGI.new
236247
# cgi.cookies['name'].expires = Time.now + 30
237248
# cgi.out("cookie" => cgi.cookies['name']) {"string"}
238249
#
239250
# === Print http header and html string to $DEFAULT_OUTPUT ($>)
240251
#
241-
# require "cgi"
242252
# cgi = CGI.new("html4") # add HTML generation methods
243253
# cgi.out do
244254
# cgi.html do

0 commit comments

Comments
 (0)