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:
01: #!/usr/local/bin/ruby 02: ...