I have a suspicion that there are a lot of web developers out there who, like me, are eager to start learning more about Ruby but who are stunted by the incredible amount of unfamiliar conventions that are used in Rails. For many of us, our first introduction to Ruby was (or is) through Rails and the challenge of learning both Ruby and Rails at once should not be underestimated; frankly it’s damn hard.
Thankfully, there’s an easier way. Especially if you’re accustomed to “old-school” development workflows that you learned through, say, PHP or perhaps Perl scripting, then ditching Rails and starting purely with Ruby might be the way to go. Doing things on your own without the “magic” of Rails will let you learn Ruby in a simple, yet fully-featured environment, much like the way you might have learned classic CGI programming.
Specifically, by using eRuby, the Embedded Ruby interpreter, you can make Ruby web development feel just like PHP development. Install it, upload an HTML file with some embedded Ruby code in it, and—voila—you have a single-page web application.
Why this tutorial?
Hivemind’s Getting Started with Ruby on the Web is an excellent resource that described this process from start-to-finish for Windows users. In fact, it was after finding that article that I realized web development with Ruby didn’t have to be governed by Rails and, being a web developer like the authors of Hiveminds, I wanted an easy way to use my Ruby knowledge on the Web.
However, I couldn’t find any similar tutorial or walkthrough for those of us who use Macs. Even the Linux walkthroughs are lacking (or at least my Googling skill for them is). After spending a little while successfully getting eRuby set up on my Mac running Mac OS X 10.4 Tiger, I thought I’d share what I learned with others, so I wrote this article. Its content mirrors the one from Hiveminds to a great degree but focuses on getting eRuby installed on Mac OS X instead of Windows (obviously). I imagine these instructions should be pretty similar if not identical for any other *nix-like platform.
Getting eRuby installed and configured with your web server is actually pretty straightforward and won’t take an experienced web developer much time at all. All we’re going to need is the Apache web server and the Ruby programming language to start, both of which are already pre-installed on Mac OS X 10.4 Tiger. (And in Mac OS X 10.5 Leopard, Apple is even shipping new Macs with Ruby On Rails preconfigured, but that’s a subject for another time.) However, Apple typically ships an older copy of Ruby than is readily available, so you might choose to use a newer version of Ruby that you download yourself.
The easiest way to get a copy of eRuby (and an updated Ruby, if you want it) is with a package manager such as MacPorts. With MacPorts installed (which is a simple, standard package installation downloadable from the MacPorts home page), simply run
sudo port install eruby
from your terminal to download the latest stable Ruby and eRuby in one fell swoop. After this runs, you’ll have a new Ruby in
/opt/local/bin/ruby and an eRuby in
/opt/local/bin/eruby. Alternatively, of course, you could compile and install eRuby yourself. Instructions for that are on the eRuby homepage.
As a quick test to see if everything is installed and working correctly, create a plain text file with your favorite editor that contains the following code:
Hello world! The time is now <%= Time.now %>.
All this does is print out a “Hello world!” greeting and then announces the current time (as reported by your system clock, of course).
If you named the above file helloworld.rhtml then you can feed this file to the
eruby interpreter as follows:
Note that the file extension doesn’t actually matter. You could use
.erb, if you like, or any other arbitrary file extension. The file extension is, as you’ll see later, only used for telling Apache which kind of file it is. So once you come up with file extension you like, stick with it. The standard file extensions are either
.erb, so I’d recommend using one of those. (I prefer
.erb, myself, since I don’t necessarily only want to create HTML pages. I might want to create XML documents like news feeds or even XML databases, so
.erb seems a more reasonable file name extension, though most of the eRuby documentation uses
If things worked properly, you should see output similar to the following:
Hello world! The time is now Wed Oct 24 21:58:54 -0400 2007.
Serve it up!
Now that eRuby is installed and working, the next step is to set it up as a CGI so that when your web server (Apache, in my case) fetches the file it will first feed it through eRuby for processing before sending it back to the browser. The simplest way to do this is to configure Apache to use a specific directory somewhere on your filesystem as a CGI directory—that is, a directory whose contents are all treated as CGI programs. You do this via Apache’s
Conveniently, standard installations of Mac OS X already come with a CGI directory. It’s located at
/Library/WebServer/CGI-Executables. This directory is just like any other, except that Apache treats it specially. It treats it specially because Apache’s configuration file,
/etc/httpd/httpd.conf contains a line of text that reads as follows. (On a standard installation of Mac OS X client, this is line 671 of the file.)
ScriptAlias /cgi-bin/ "/Library/WebServer/CGI-Executables/"
This line basically just tells apache, “Treat every file in the
/Library/WebServer/CGI-Executables directory as though it were a CGI program.” So, since that’s how we’re treating eRuby, we’re going to want to put our
eruby binary in that directory. However, we don’t actually want to make copies of the binary. Having too many copies of a program on your system can easily get confusing. So instead, we’ll make a symbolic link (or an alias to use the classic Mac terminology) to the binary.
ln -s /opt/local/bin/eruby /Library/WebServer/CGI-Executables/eruby
AddHandler simply tells Apache to treat files with a certain extension in a certain way. For instance, you can say something like, “Treat all files ending in
.php as PHP scripts.” Similarly, you can (and will) tell Apache, “Treat all files ending in
.erb as eRuby CGI pages.”
To do this, we add the following line to our
/etc/httpd/users/username.conf file somewhere inside the
</Directory> block. Naturally, replace username with, of course, your Mac OS X user’s short name:
AddHandler rubypage .erb .rhtml
Again, this just tells Apache that files ending in
.rhtml should be treated the same way other
rubypages are treated. But how are eRuby pages supposed to be treated? They’re supposed to be handed off to the eRuby program, of course, so we need an
Action directive that applies to the
rubypage handler we just defined.
Right below the above line, add the following line, too:
Action rubypage /cgi-bin/eruby
This line tells Apache that any file that it is treating as a “rubypage” file should be sent to the program at the URL accessible at
/cgi-bin/eruby, which we’ve defined earlier to be our symbolic link to the
Your completed user-specific Apache configuration file should now look something like this:
<Directory "/Users/username/Sites/"> … # Let eRuby files get parsed through the proper CGI binary AddHandler rubypage .erb .rhtml Action rubypage /cgi-bin/eruby </Directory>
Are You Being Served?
That’s it, everything should now be in place. To make sure, move your earlier
helloworld.rhtml file into your user’s
Sites folder, and now simply point your browser to
http://localhost/~username/helloworld.rhtml (again, replacing username with your Mac OS X user short name, of course). Congratulations, you’re serving up Ruby-coded applications via eRuby.
This is just the set up, of course. Now the real fun begins: learning Ruby. Here are some suggested places to start.
- Programming Ruby: Ruby and the Web — A chapter out of the free book Programming Ruby all about Ruby as a CGI programming language.
- Hiveminds: eRuby: How to use Ruby CGI and ERB templating for Web Pages — a quick’n’dirty article explaining the basics of using Ruby on web pages in classic CGI style where program logic and HTML templates are kept separated, as opposed to classic PHP style, where the two are mingling together on one page.
- COOLNAMEHERE: Simple Ruby CGI — Like the Hiveminds example above, another quick’n’dirty Ruby CGI how-to.
- cgi: Ruby Standard Library Documentation — Ruby’s standard library contains a rich set of features, and the CGI objects are among them. They are well-documented in the official Ruby Documentation Project.
Naturally, feel free to leave comments if you have more resources that you found helpful.