This article is intended to provide a casual introduction the CGI programming with the Ruby language. You won’t be an expert when you are done, but you will certainly be ready to explore more on your own, and maybe delve into becoming a Ruby/CGI expert.
Why Are You Reading This?
There are a couple of possibilities, other than the obvious “I have nothing better to do”. That’s my excuse, incidentally, so you better pick another. Perhaps you have been using Ruby for a little while, and you want to learn how to use this curious language as a Web programming tool. There is also the possibility that you are a grizzled Perl/CGI veteran of many years, and you are interested in learning how this curious language handles tasks that you do every day with CGI.pm. Hopefully neither of you will be too disappointed by my efforts.
Even though I hesitate to call it my favorite language, I definitely prefer to do most of my coding in Ruby these days. It is similar to my other favorite, Perl, because it is put together in such a way that programs are easy to write. I must admit that large programs are easier to manage in Ruby, though. I use object-oriented programming techniques to abstract and tame the complexity of particularly challenging projects. Support for object-oriented programming is built into the Ruby language, so extremely large and complex applications are relatively simple to build and maintain.
I’ll give you the easy answer first: I chose to write an article on CGI programming with Ruby because CGI programming is what I know best. I’ve been doing CGI for nearly ten years now, and can comfortably answer (or find the answer to) most questions on the subject. “Write about what you know” and all that.
The longer answer is that CGI is an established standard, and many of the more powerful Web development tools use CGI concepts as their basis. You will be able to create some impressive Web applications using CGI. CGI is easy to set up on a Web server, and this means that CGI has the lowest barrier to entry. I think that’s the term - I never have been up on my buzzwords. Even if you move on to something else later, knowing a little bit about how CGI works will give you a better appreciation for the other tools that are available for you.
CGI is a standard part of the Ruby library. All you need to create a CGI application with Ruby is a standard installation of Ruby and a server capable of processing CGI requests. This includes nearly every Web server in existence, but I will focus my attention on the Apache Web server. Apache is freely available for most operating systems, and should be very easy to install from the directions at the Apache web site.
CGI programs are so easy because they are basically just a bunch of
“Hello World” The CGI Way
Let’s start with a basic task, pushing some plain old HTML out to the browser. It isn’t fancy, but it’ll do to help us get a feel for things.
#!/usr/bin/ruby puts "Content-Type: text/html" puts puts "<html>" puts "<body>" puts "<h1>Hello Ruby!</h1>" puts "</body>" puts "</html>"