File: web.html

package info (click to toggle)
rubybook 0.2.1-1
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k, lenny, squeeze, wheezy
  • size: 4,248 kB
  • ctags: 1,042
  • sloc: xml: 60,486; makefile: 25
file content (591 lines) | stat: -rw-r--r-- 24,023 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
<html><title>Programming Ruby: The Pragmatic Programmer's Guide</title><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><STYLE TYPE="text/css"><!--
       BODY    { margin-left: 1in;
                 width: 6in;
                 font-family: helvetica, arial, sans-serif;
               }
       H1      { color: #000080;
                 font-family: helvetica, arial, sans-serif;
                 font-size: 22pt;
                 margin-left: 0in
               }
       H2      { color: #000080;  font: bold x-large helvetica, sans-serif;
                 margin-left: 0in }
       H3      { color: #000080;  font: bold   large helvetica, sans-serif; }
       H4      { color: #000080;  font: italic large helvetica, sans-serif; }
       .ruby   { background: #fff0f0 }
       .header { color: white }
       .subheader { color: #ffdddd }
       .sidebar   { width: 6in }
       span.sans { font-family: helvetica, arial, sans-serif }
       -->
   </STYLE><table bgcolor="#a03030" cellpadding="3" border="0" cellspacing="0"><tr><td colspan="3"><table bgcolor="#902020" cellpadding="20"><tr><td><h1 class="header">Programming Ruby</h1><h3 class="subheader">The Pragmatic Programmer's Guide</h3></td></tr></table></td></tr><tr><td width="33%" align="left"><a class="subheader" href="rubyworld.html">Previous &lt;</a></td><td width="33%" align="center" valign="middle"><a class="subheader" href="index.html">Contents ^</a><br></td><td width="33%" align="right"><a class="subheader" href="ext_tk.html">Next ></a><br></td></tr></table></head><body bgcolor="white">
<!--
    Copyright (c) 2001 by Addison Wesley Longman.  This
    material may be distributed only subject to the terms and
    conditions set forth in the Open Publication License, v1.0 or
    later (the latest version is presently available at
    http://www.opencontent.org/openpub/).
-->
<h1>Ruby and the Web</h1><hr><br>
<P></P>
Ruby is no stranger to the Internet.  Not only can you write your own
SMTP server, FTP daemon, or Web server in Ruby, but you can also use
Ruby for more usual tasks such as CGI programming or as a
replacement for PHP.
<h2>Writing CGI Scripts</h2>
<P></P>
You can use Ruby to write CGI scripts quite easily.  To have a Ruby
script generate HTML output, all you need is
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
#!/usr/bin/env&nbsp;ruby
print&nbsp;"HTTP/1.0&nbsp;200&nbsp;OK\r\n"
print&nbsp;"Content-type:&nbsp;text/html\r\n\r\n"
print&nbsp;"&lt;html>&lt;body>Hello&nbsp;World!&lt;/body>&lt;/html>\r\n"
</pre></td></tr></table>

<P></P>
You <em>could</em> use Ruby's regular expression features to parse
incoming query strings, look up environment variables, check tags,
substitute text into templates, escape special characters, format up
the HTML, and print it all out.
<P></P>
Or, you could use class <code>CGI</code>.
<h3>Using cgi.rb</h3>
<P></P>
Class <code>CGI</code> provides support for writing CGI scripts.  With it, you
can manipulate forms, cookies, and the environment, maintain stateful
sessions, and so on.  It's documented in full in the reference section
beginning on page 501, but we'll take a quick look at its
capabilities here.
<h3>Quoting</h3>
<P></P>
When dealing with URLs and HTML code, you must be careful to quote
certain characters.  For instance, a slash character (``/'') has
special meaning in a URL, so it must be ``escaped'' if it's not part
of the path name.  That is, any ``/'' in the query portion of the URL
will be translated to the string ``<code>%2F</code>'' and must be translated back
to a ``/'' for you to use it.  Space and ampersand are also special
characters. To handle this, <code>CGI</code> provides the routines
<code>CGI#escape</code> and <code>CGI#unescape</code>:
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
require&nbsp;'cgi'
puts&nbsp;CGI.escape(&nbsp;"Nicholas&nbsp;Payton/Trumpet&nbsp;&amp;&nbsp;Flugel&nbsp;Horn"&nbsp;)
</pre></td></tr></table>

<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
Nicholas+Payton%2FTrumpet+%26+Flugel+Horn
</pre></td></tr></table>

<P></P>
Similarly, you may want to escape HTML special characters:
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
require&nbsp;'cgi'
puts&nbsp;CGI.escapeHTML(&nbsp;'&lt;a&nbsp;href="/mp3">Click&nbsp;Here&lt;/a>'&nbsp;)
</pre></td></tr></table>

<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
&amp;lt;a&nbsp;href=&amp;quot;/mp3&amp;quot;&amp;gt;Click&nbsp;Here&amp;lt;/a&amp;gt;
</pre></td></tr></table>

<P></P>
To get really fancy, you can decide to escape only certain elements
within a string:
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
require&nbsp;'cgi'
puts&nbsp;CGI.escapeElement('&lt;hr>&lt;a&nbsp;href="/mp3">Click&nbsp;Here&lt;/a>&lt;br>','A')
</pre></td></tr></table>

<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
&lt;hr>&amp;lt;a&nbsp;href=&amp;quot;/mp3&amp;quot;&amp;gt;Click&nbsp;Here&amp;lt;/a&amp;gt;&lt;br>
</pre></td></tr></table>

<P></P>
Here only the ``<code>A</code>'' tag is escaped; other tags are left alone.
Each of these methods has an ``<code>un</code>-'' version to restore the original
string.
<h3>Forms</h3>
<P></P>
Using class <code>CGI</code> gives you access to HTML query parameters in two
ways.
Suppose we are given a URL of
<code>/cgi-bin/lookup?player=Miles%20Davis&amp;year=1958</code>.  You can access
the parameters ``<code>player</code>'' and ``<code>year</code>'' using <code>CGI#[]</code> directly:
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>require&nbsp;'cgi'</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>cgi&nbsp;=&nbsp;CGI.new</code></td>
</tr>
<tr>
  <td valign="top"><code>cgi['player']</code></td>
  <td valign="top"></td>
  <td valign="top"><code>["Miles&nbsp;Davis"]</code></td>
</tr>
<tr>
  <td valign="top"><code>cgi['year']</code></td>
  <td valign="top"></td>
  <td valign="top"><code>["1958"]</code></td>
</tr>
</table>
<P></P>

<P></P>
Or, you can retrieve all parameters as a <code>Hash</code>:
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>require&nbsp;'cgi'</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>cgi&nbsp;=&nbsp;CGI.new</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>h&nbsp;=&nbsp;cgi.params</code></td>
</tr>
<tr>
  <td valign="top"><code>h['player']</code></td>
  <td valign="top"></td>
  <td valign="top"><code>["Miles&nbsp;Davis"]</code></td>
</tr>
</table>
<P></P>

<h3>Creating Forms and HTML</h3>
<P></P>
<code>CGI</code> contains a huge number of methods used to create HTML---one
method per tag.  In order to enable these methods, you must create a
<code>CGI</code> object by calling <code>CGI#new</code>, passing in the required level
of HTML.  For these examples, we'll use ``<code>html3</code>''.
<P></P>
To make tag nesting easier, these methods take their content as code
blocks.  The code blocks should return a <code>String</code>, which will be
used as the content for the tag.  For this example, we've added some
gratuitous newlines to make the output fit on the page.
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
require&nbsp;"cgi"
cgi&nbsp;=&nbsp;CGI.new("html3")&nbsp;&nbsp;#&nbsp;add&nbsp;HTML&nbsp;generation&nbsp;methods
cgi.out{
&nbsp;&nbsp;cgi.html{
&nbsp;&nbsp;&nbsp;&nbsp;cgi.head{&nbsp;"\n"+cgi.title{"This&nbsp;Is&nbsp;a&nbsp;Test"}&nbsp;}&nbsp;+
&nbsp;&nbsp;&nbsp;&nbsp;cgi.body{&nbsp;"\n"+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.form{"\n"+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.hr&nbsp;+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.h1&nbsp;{&nbsp;"A&nbsp;Form:&nbsp;"&nbsp;}&nbsp;+&nbsp;"\n"+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.textarea("get_text")&nbsp;+"\n"+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.br&nbsp;+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.submit
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}
}
</pre></td></tr></table>

<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
Content-Type:&nbsp;text/html
Content-Length:&nbsp;302
<P></P>
&lt;!DOCTYPE&nbsp;HTML&nbsp;PUBLIC&nbsp;"-//W3C//DTD&nbsp;HTML&nbsp;3.2&nbsp;Final//EN">&lt;HTML>&lt;HEAD>
&lt;TITLE>This&nbsp;Is&nbsp;a&nbsp;Test&lt;/TITLE>&lt;/HEAD>&lt;BODY>
&lt;FORM&nbsp;METHOD="post"&nbsp;ENCTYPE="application/x-www-form-urlencoded">
&lt;HR>&lt;H1>A&nbsp;Form:&nbsp;&lt;/H1>
&lt;TEXTAREA&nbsp;COLS="70"&nbsp;NAME="get_text"&nbsp;ROWS="10">&lt;/TEXTAREA>
&lt;BR>&lt;INPUT&nbsp;TYPE="submit">&lt;/FORM>&lt;/BODY>&lt;/HTML>
</pre></td></tr></table>

<P></P>
This code will produce an HTML form titled ``This Is a Test,''
followed by a horizontal rule, a level-one header, a test input area,
and finally a submit button.  When the submit comes back, you'll have
a CGI parameter named ``<code>get_text</code>'' containing the text the 
user entered.
<h3>Cookies</h3>
<P></P>
You can store all kinds of interesting stuff on an unsuspecting
surfer's machine using <em>cookies</em>.
You can create a named cookie
object and store a number of values in it.  To send it down to the
browser, set a ``cookie'' header in the call to <code>CGI#out</code>.
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
require&nbsp;"cgi"
cookie&nbsp;=&nbsp;CGI::Cookie.new("rubyweb",&nbsp;"CustID=123",&nbsp;"Part=ABC");
cgi&nbsp;=&nbsp;CGI.new("html3")
cgi.out(&nbsp;"cookie"&nbsp;=>&nbsp;[cookie]&nbsp;){
&nbsp;&nbsp;cgi.html{
&nbsp;&nbsp;&nbsp;&nbsp;"\nHTML&nbsp;content&nbsp;here"
&nbsp;&nbsp;}
}
</pre></td></tr></table>

<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
Content-Type:&nbsp;text/html
Content-Length:&nbsp;86
Set-Cookie:&nbsp;rubyweb=CustID%3D123&amp;Part%3DABC;&nbsp;path=
<P></P>
&lt;!DOCTYPE&nbsp;HTML&nbsp;PUBLIC&nbsp;"-//W3C//DTD&nbsp;HTML&nbsp;3.2&nbsp;Final//EN">&lt;HTML>
HTML&nbsp;content&nbsp;here&lt;/HTML>
</pre></td></tr></table>

<P></P>
The next time the user comes back to this page, you can retrieve
the cookie values for <code>CustID</code> and <code>Part</code>, as shown in the
HTML output.
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
require&nbsp;"cgi"
cgi&nbsp;=&nbsp;CGI.new("html3")
cgi.out{
&nbsp;&nbsp;cgi.html{
&nbsp;&nbsp;&nbsp;&nbsp;cgi.pre{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cookie&nbsp;=&nbsp;cgi.cookies["rubyweb"]
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"\nCookies&nbsp;are\n"&nbsp;+&nbsp;cookie.value.join("\n")
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}
}
</pre></td></tr></table>

<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
Content-Type:&nbsp;text/html
Content-Length:&nbsp;111
<P></P>
&lt;!DOCTYPE&nbsp;HTML&nbsp;PUBLIC&nbsp;"-//W3C//DTD&nbsp;HTML&nbsp;3.2&nbsp;Final//EN">&lt;HTML>&lt;PRE>
Cookies&nbsp;are
CustID=123
Part=ABC&lt;/PRE>&lt;/HTML>
</pre></td></tr></table>

<h3>Sessions</h3>
<P></P>
Cookies by themselves still need a bit of work to be useful.
What
we really want is a <em>session:</em> a persistent state for some Web
surfer.  Sessions are handled with <code>CGI::Session</code>
(documented beginning on page 508), which uses cookies
but provides a higher-level abstraction.
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
<P></P>
require&nbsp;"cgi"
require&nbsp;"cgi/session"
<P></P>
cgi&nbsp;=&nbsp;CGI.new("html3")
sess&nbsp;=&nbsp;CGI::Session.new(&nbsp;cgi,&nbsp;"session_key"&nbsp;=>&nbsp;"rubyweb",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"session_id"&nbsp;&nbsp;=>&nbsp;"9650",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"new_session"&nbsp;=>&nbsp;true,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"prefix"&nbsp;=>&nbsp;"web-session.")
sess["CustID"]&nbsp;=&nbsp;123
sess["Part"]&nbsp;=&nbsp;"ABC"
<P></P>
cgi.out{
&nbsp;&nbsp;cgi.html{
&nbsp;&nbsp;&nbsp;&nbsp;"\nHTML&nbsp;content&nbsp;here"
&nbsp;&nbsp;}
}
</pre></td></tr></table>

<P></P>
This will send a cookie to the user named ``rubyweb'' with a value of
9650.  It will also create a disk file in <code>$TMP/web-session.9650</code>
with the <em>key, value</em> pairs for <code>CustID</code> and <code>Part</code>.
<P></P>
When the user returns, all you need is a parameter to indicate the
session id. In this example, that would be <code>rubyweb=9650</code>.  With
that value in the parameters, you'll be able to retrieve the full
set of saved session data.
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
require&nbsp;"cgi"
require&nbsp;"cgi/session"
<P></P>
cgi&nbsp;=&nbsp;CGI.new("html3")
sess&nbsp;=&nbsp;CGI::Session.new(&nbsp;cgi,&nbsp;"session_key"&nbsp;=>&nbsp;"rubyweb",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"prefix"&nbsp;=>&nbsp;"web-session.")
cgi.out{
&nbsp;&nbsp;cgi.html{
&nbsp;&nbsp;&nbsp;&nbsp;"\nCustomer&nbsp;#{sess['CustID']}&nbsp;orders&nbsp;an&nbsp;#{sess['Part']}"
&nbsp;&nbsp;}
}
</pre></td></tr></table>

<h2>Embedding Ruby in HTML</h2>
<P></P>
So far we've looked at using Ruby to create HTML output, but we can
turn the problem inside out; we can actually embed Ruby in an HTML
document.
<P></P>
There are a number of packages that allow you to embed Ruby
statements in some other sort of a document, especially in an HTML
page. Generically, this is known as ``eRuby.''  Specifically, there
are several different implementations of eRuby, including <code>eruby</code>
and <code>erb</code>.  The remainder of this section will discuss <code>eruby</code>,
written by Shugo Maeda.
<P></P>
Embedding Ruby in HTML is a very powerful concept---it basically gives
us the equivalent of a tool such as ASP, JSP, or PHP, but with the
full power of Ruby.
<h3>Using eruby</h3>
<P></P>
<code>eruby</code> acts as a filter, plain and simple.  Any text within the input
file is passed through untouched, with the following exceptions:
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3">
<tr bgcolor="#ff9999">
  <td valign="top"><b>Expression</b></td>
  <td valign="top"><b>Description</b></td>
</tr>
<tr>
  <td valign="top"><code>&lt;%</code> <em>ruby code</em> <code>%></code></td>
  <td valign="top">The Ruby code between the delimiters 
  is replaced with its output.</td>
</tr>
<tr>
  <td valign="top"><code>&lt;%=</code> <em>ruby expression</em> <code>%></code></td>
  <td valign="top">The Ruby expression
  between the delimiters is replaced with its value.</td>
</tr>
<tr>
  <td valign="top"><code>&lt;%#</code> <em>ruby code</em> <code>%></code></td>
  <td valign="top">The Ruby code between the
  delimiters is ignored (useful for testing).</td>
</tr>
<tr><td colspan="9" bgcolor="#ff9999" height="2"><img src="dot.gif" width="1" height="1"></td></tr></table>
<P></P>
You invoke <code>eruby</code> as:
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500"><tr><td><pre>
eruby <i>[</i><i>options</i><i>]</i> <i>[</i><i>document</i><i>]</i>
</pre></td></tr></table>
<P></P>
If the <em>document</em> is omitted, <code>eruby</code> will read from standard
input.  The command-line options for <code>eruby</code> are shown in Table
14.1 on page 151.
<table border="2" width="500" bgcolor="#ffe0e0"><tr><td>
<b>Command-line options for <code>eruby</code></b>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3">
<tr bgcolor="#ff9999">
  <td valign="top"><b>Option</b></td>
  <td valign="top"><b>Description</b></td>
</tr>
<tr>
  <td valign="top"><code>-d</code>, <code>--debug</code></td>
  <td valign="top">Sets $DEBUG to <code>true</code>.</td>
</tr>
<tr>
  <td valign="top"><code>-K</code><em>kcode</em></td>
  <td valign="top">Specifies an alternate coding system
                                    (see page 139).</td>
</tr>
<tr>
  <td valign="top"><code>-M</code><em>mode</em></td>
  <td valign="top">Specifies runtime <em>mode</em>, one of:
<P></P>
  <table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3">
<tr>
  <td valign="top"><code>f</code></td>
  <td valign="top">filter mode</td>
</tr>
<tr>
  <td valign="top"><code>c</code></td>
  <td valign="top">CGI mode (prints errors as HTML, sets $SAFE=1)</td>
</tr>
<tr>
  <td valign="top"><code>n</code></td>
  <td valign="top">NPH-CGI mode (prints extra HTTP headers, sets $SAFE=1)</td>
</tr>
<tr>
  <td valign="top"></td>
</tr>
</table>
</td>
</tr>
<tr>
  <td valign="top"><code>-n</code>, <code>--noheader</code></td>
  <td valign="top">Disables CGI header output.</td>
</tr>
<tr>
  <td valign="top"><code>-v</code>, <code>--verbose</code></td>
  <td valign="top">Enables verbose mode.</td>
</tr>
<tr>
  <td valign="top"><code>--version</code></td>
  <td valign="top">Prints version information and exits.</td>
</tr>
<tr><td colspan="9" bgcolor="#ff9999" height="2"><img src="dot.gif" width="1" height="1"></td></tr></table>
<P></P>
</td></tr></table>
<P></P>
Let's look at some simple examples.  We'll run the <code>eruby</code> executable 
on the following input.
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
This&nbsp;text&nbsp;is&nbsp;&lt;%&nbsp;a&nbsp;=&nbsp;100;&nbsp;puts&nbsp;"#{a}%&nbsp;Live!"&nbsp;%>
</pre></td></tr></table>

<P></P>
<code>eruby</code> substitutes the expression between the delimiters and
produces
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
This&nbsp;text&nbsp;is&nbsp;100%&nbsp;Live!
</pre></td></tr></table>

<P></P>
Using the &lt;%= form acts as if you printed the value of the
expression.  For instance, the input
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
&lt;%a&nbsp;=&nbsp;100%>This&nbsp;text&nbsp;is&nbsp;almost&nbsp;&lt;%=a%>&nbsp;degrees!&nbsp;Cool!
</pre></td></tr></table>

<P></P>
replaces the <code>=a</code> with the value of <code>a</code>.
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
This&nbsp;text&nbsp;is&nbsp;almost&nbsp;100&nbsp;degrees!&nbsp;Cool!
</pre></td></tr></table>

<P></P>
And, of course, you can embed Ruby within a more complex document type,
such as HTML.
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
&lt;!DOCTYPE&nbsp;HTML&nbsp;PUBLIC&nbsp;"-//W3C//DTD&nbsp;HTML&nbsp;4.01//EN">
&lt;html>
&lt;head>
&lt;title>eruby&nbsp;example&lt;/title>
&lt;/head>
&lt;body>
&lt;h1>Enumeration&lt;/h1>
&lt;ul>
&lt;%(1..10).each&nbsp;do|i|%>
&nbsp;&nbsp;&lt;li>number&nbsp;&lt;%=i%>&lt;/li>
&lt;%end%>
&lt;/ul>
&lt;h1>Environment&nbsp;variables&lt;/h1>
&lt;table>
&lt;%ENV.keys.sort.each&nbsp;do&nbsp;|key|%>
&nbsp;&nbsp;&lt;tr>
&nbsp;&nbsp;&nbsp;&nbsp;&lt;th>&lt;%=key%>&lt;/th>&lt;td>&lt;%=ENV[key]%>&lt;/td>
&nbsp;&nbsp;&lt;/tr>
&lt;%end%>
&lt;/table>
&lt;/body>
&lt;/html>
</pre></td></tr></table>

<h3>Installing eruby in Apache</h3>
<P></P>
You can set up an Apache Web server to automatically parse
Ruby-embedded documents using eRuby, much in the same way that PHP
does.  You create Ruby-embedded files with an ``<code>.rhtml</code>'' suffix
and configure the Web server to run the <code>eruby</code> executable on these
documents to produce the desired HTML output.
<P></P>
In order to use <code>eruby</code> with the Apache Web server, you need to
perform the following steps.
<P></P>
<ul>
  <li> Copy the <code>eruby</code> binary to the <code>cgi-bin</code> directory.
<P></P>
  </li><li> Add the following two lines to <code>httpd.conf</code>:
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
AddType&nbsp;application/x-httpd-eruby&nbsp;.rhtml
Action&nbsp;application/x-httpd-eruby&nbsp;/cgi-bin/eruby
</pre></td></tr></table>

<P></P>
</li><li> If desired, you can also add or replace the <code>DirectoryIndex</code>
  directive such that it includes <code>index.rhtml</code>.  This lets you use
  Ruby to create directory listings for directories that do not
  contain an <code>index.html</code>.  For instance, the following directive
  would cause the embedded Ruby script <code>index.rhtml</code> to be searched
  for and served if neither <code>index.html</code> nor <code>index.shtml</code> existed in a
  directory.
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
DirectoryIndex&nbsp;index.html&nbsp;index.shtml&nbsp;index.rhtml
</pre></td></tr></table>

<P></P>
  Of course, you could also simply use a site-wide Ruby script as
  well.
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
DirectoryIndex&nbsp;index.html&nbsp;index.shtml&nbsp;/cgi-bin/index.rb
</pre></td></tr></table>

<P></P>
</li></ul>
<P></P>
And that's it!  You can now write HTML documents that contain embedded
Ruby to generate forms and content dynamically.  Be sure to see also
the Ruby <code>CGI</code> library, which is documented
beginning on page 501.  
<h2>Improving Performance</h2>
<P></P>
You can use Ruby to write CGI programs for the Web, but, as with most
CGI programs, the default configuration has to start up a new copy of
Ruby with every cgi-bin page access.
That's expensive in terms of
machine utilization and can be painfully slow for Web surfers.  
The Apache Web server solves this problem by allowing loadable
<em>modules</em>.
<P></P>
Typically, these modules are dynamically loaded and become part of the 
running Web server process---there is no need to spawn
another interpreter over and over again to service requests; the Web
server <em>is</em> the interpreter.
<P></P>
And so we come to <code>mod_ruby</code> (available from the archives), an
Apache module that links a full Ruby interpreter into the Apache Web
server itself.  The <code>README</code> file included with <code>mod_ruby</code> provides
details on how to compile and install it.
<P></P>
Once installed and configured, you can run Ruby scripts just like you
could without <code>mod_ruby</code>, except that now they will come up much
faster.
<P></P>

<p></p><hr><table bgcolor="#a03030" cellpadding="10" border="0" cellspacing="0"><tr><td width="33%" align="left"><a class="subheader" href="rubyworld.html">Previous &lt;</a></td><td width="33%" align="center" valign="middle"><a class="subheader" href="index.html">Contents ^</a><br></td><td width="33%" align="right"><a class="subheader" href="ext_tk.html">Next ></a><br></td></tr></table><p></p><font size="-1">Extracted from the book "Programming Ruby -
     The Pragmatic Programmer's Guide"</font><br><font size="-3">
      Copyright
      &#169;
      2000 Addison Wesley Longman, Inc. Released under the terms of the
      <a href="http://www.opencontent.org/openpub/">Open Publication License</a> V1.0.
        <br>
      This reference is available for
        <a href="http://www.pragmaticprogrammer.com/ruby/downloads/book.html">download</a>.
   </font></body></html>