O'Reilly logo

Sams Teach Yourself Ruby in 21 Days by Mark Slagell

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

Object-Oriented CGI Support

HTML is not much fun to write by hand, and writing program code to produce it via print statements can be even less fun. Mistakes in the code might go unnoticed if you test on one browser and not others; malformed code might be interpreted charitably by one browser but cause another to crash.

The CGI class brings some order into the potential chaos of HTML generation by tying the page code into the structure of a Ruby program. One immediate benefit is that it enforces correct nesting of tags, so that mistakes like <center><em>this</center></em> can't happen. Here are two equivalent CGI scripts. The first, in Listing 20.9, is written the brute-force way:

Listing 20.9. cgi-bin/cgi-test.rb
 01: #!/usr/local/bin/ruby 02: ...

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