Why Ruby is an acceptable Perl

Everyone has read this article. It explains how Ruby’s enumerable effectively makes it a lisp. Like all we arrogant lispers, I of course disagree. That’s not to say that Ruby isn’t a good language, though. It’s just that it’s more idiomatic of Perl than Lisp.

It does have its big selling point, enumerable. Enumerable is nice. Blocks are nice, although they are not as powerful as lambdas. One big selling point that you rarely see mentioned, though, is Perl-style regexp.

Nearly every language can do regular expression matching using Perl-style regular expressions. However, few have built-in operators for it – =~ to match an expression and s/foo/bar to modify an expression. PHP’s functions, by comparison, are a huge code overhead. The same is true for Python. For example, in PHP:

$x = "hello world";
$x = preg_replace("/world/", "Jeff");

…not to mention having to first import the regex library into Python:

import re
regex = re.compile('world')
x = "hello world"
x = regex.sub("Jeff", x)

Whereas in Perl you just have:

$x = "hello world";
$x =~ s/world/Jeff/;

Ruby has similar syntax and a powerful regexp library. It also has Perl’s syntactic freedom – that is, things can be written in many different ways.

i = "Hello world"
if i == "Hello world"
    puts i
end

or…

puts i if i == "Hello world"

Functions also do not require parenthesis if the meaning is clear:

puts(i)

or…

puts i

This is often called “poetry mode” by Rubyists. However, few Rubyists like to compare their language with Perl because Ruby is religiously object oriented, and Perl is more of an example on how not to implement OOP.

But Ruby does have its origins in Perl. It inherits a lot of features from languages like Smalltalk and lisp, but if you look at a large Ruby program, you begin to see how Perl-like it is.

And that is Ruby’s biggest problem as well. As with Perl, syntactic freedom means that the neatness of code is often a product of the mood of the programmer or the deadline. Coming back to a program after six months can mean hours of analyzing code, trying to figure out what on earth you were thinking when you wrote that.

Leave a comment | Trackback
May 31st, 2007 | Posted in Soap box
Tags: ,