Ruby is sweet
Thursday, July 03, 2003 04:25 PM

Before I start the Ruby lovefest... regarding my previous post on Ruby, I found this tidbit from a FAQ entry:

Ruby's syntax and design philosophy are heavily influenced by Perl. It has a lot of syntactic variability. Statement modifiers (if, unless, while, until, etc.) may appear at the end of any statement. Some key words are optional (the ``then'' in an ``if'' statement for example). Parentheses may sometimes be elided in method calls. The receiver of a method may usually be elided. Many, many things are lifted directly from Perl. Built in regular expressions, $_ and friends, here documents, the single-quoted / double-quoted string distinction, $ and @ prefixes to distinguish different kinds of names and so forth.

I knew it! Is it just me, or is Perl at the root of all things evil!? ;)

All kidding aside, I have been very impressed with Ruby overall. It is as convenient as Python, but feels more coherent--some of Python's features kind of seem arbitrary or grafted on, as opposed to Ruby where a smaller number of constructs are more widely applicable.

I like that Ruby uses backquotes to `execute any arbitrary system command`--that's one Perlism that I wish all scripting languages followed.

I like blocks and iterators a lot.

I like how almost every statement returns some kind of expression.

I simply love eRuby. I don't know how many web scripting languages I've used over the years, but as far as basic syntax goes, I think Ruby-impregnated HTML is by far the best solution for me. I have been longing for a language that is fairly powerful (i.e. not a stripped down "templating language" like Velocity or CityScript), doesn't result in ugly code (see looping over collections in JSP), lets you do includes, and can (optionally) be used at "compile time" to create a static set of HTML documents.

(Note: This is a pretty unfashionable set of requirements these days--the trend is to move the power out of the hands of web developers and into dedicated logic classes, thus enabling better role separation. By taking the real code out of the HTML, you can hire (cheap) HTML developers who don't know how to write real code. Well, I know how to code, and I often need to create websites. Sometimes it's just really nice to be able to declare variables, perform arithmetic operations, and create and manipulate lists from right within the presentation tier. So keep your flaccid, stripped-down templating language outta my face.)

eRuby does all that and more. It is so wonderful that I'm having trouble expressing it in words.

It's just the little things. Say you have an array of images and want to filter out the ones that don't actually exist on disk. You can do this in one line:

<% imageList.delete_if { |img| File.exist?(img) } %>

Contrast that to, say, JSP:

ArrayList tmpList = new ArrayList(imageList.size());
for (Iterator it = imageList.iterator(); it.hasNext();) {
  String thisFile = (String)it.next();
  if (new File(thisFile).exists())
imageList = tmpList;

or ColdFusion:

<cfset tmpArray = ArrayNew()>
<cfloop index="image" list="imageArray">
  <cfif FileExists(image)><cfset ArrayAppend(tmpArray, image)></cfif>
<cfset imageArray = tmpArray>

This is just one example of Ruby taking one line where most other languages need five. Furthermore, it seems like the general problem of taking data and turning it into richly formatted HTML involves many of the kinds of operations that Ruby makes very easy:

It's almost as if Ruby was specifically designed to be embedded into HTML! (It wasn't.)

Also, I have found that static typing--while a huge boon in general--is less useful in this area, specifically because there is generally so much iterating and unmarshalling (from request parameters) going on, so you end up doing a lot of casting and parsing which defeats much of the static type checking anyway.

So anyway, there you have it. If nothing else, use Ruby for generating web pages. (I'm using erb as my eRuby implementation because it is small and portable.)