I have previously admired the Ruby language, albeit from a distance, and been impressed by the vigor of the Rails community. In the last week I have written a few hundred lines of Ruby code that actually do something useful and I’ll probably release (come see it at OSCON); so now I’m somewhat better educated.
What happened was, I got a paper accepted at OSCON, and since that “S” stands for “Source” I thought it would be lame to get up on stage without some, and decided that an inflexible deadline would be the right way to overcome my laziness and adult-ADD and actually learn something. So if you come to my talk, you’re going to see some (I hope) entertaining Ruby hackery, with an unusual twist, in a good cause.
What’s Good · I’ll jump to the conclusion first. For people like me, who are proficient in Perl and Java, Ruby is remarkably, perhaps irresistibly, attractive. Over the last week I’ve got an unreasonable amount of work done in a ridiculously short period of time, with lots of interruptions, in a language I previously didn’t know. It’s intuitive enough that I’ve often found myself guessing at a syntax or a method or a usage and getting it right first time.
Maybe the single biggest advantage is readability. Once you’ve got over the hump of the block/yield idiom, I find that a chunk of Ruby code shouts its meaning out louder and clearer than any other language. Anything that increases maintainability is a pearl beyond price.
In theory, Python ought to do better, lacking all those silly
end
statements cluttering up the screen. But in practice, when
I look at Python code I find my eyes distracted by a barrage of underscores
and double-quote marks. Typography is an important component of human
communication, and Ruby’s, on balance, is cleaner.
As for Java; I’ve always found it readable, and while there is too much boilerplate, at least the superfluous delimiters are just a character apiece. But because Ruby does so much more with each statement, you can get more pieces of the puzzle in the little bit of the screen that you can look at and think about comfortably. This is just one of the many reasons why fewer-lines-of-code is a Really Important Virtue.
Anyhow, I’ve been programming in C and Java for a quarter-century and I find Ruby easier to read, only a week in. (Well, except for some of _why’s code.)
Also, it doesn’t take a lot of study before you’re saving lines of code; I’ve already written about Ruby’s low barrier to entry.
Here’s another virtue that I’ve observed but can’t explain. Something about Ruby results in me writing little teeny methods; and when I write a loop that threatens to spill out of a screen I just know that the language probably provides a better way to do it. (It’s not just a virtue of dynamic languages; the Perl code that deals with an image reference in ongoing spills across more screenfuls than I want to admit.)
Of course, a language’s culture is often more important than all that technical crap. I’ve found the ruby-talk mailing list to be a fount of wisdom and friendly to ignorant newbies too.
IRB is
seriously wonderful. I have to wonder heretically why IRB and Ruby are
different programs, but that seems to be Just The Way It Is.
If you’re new to IRB, learn to use the -r
option to load up a
class or two and then poke away at methods.
What’s Mixed · Some things about Ruby are troubling me, they could be a lot better but they could be a lot worse too. I guess the leading candidate is documentation.
I’ve previously written that I think the Pickaxe book is way better than average. It’s approximately as good as the Camel book as a reference, and quite a bit better as a tutorial; I can’t compare it to Learning Perl because I already knew perl before I discovered that book. A few days and a few hundred lines of production-plausible code in, my Pickaxe is looking shopworn. And, by the way, I repeat my assertion that several of the examples used to illustrate language features also illustrate horrible programming practice.
Online, Ruby has ri
, which is nowhere near as good as either
perldoc
or the practice of right-clicking on anything in a
NetBeans window and saying “Show Javadocs” (I assume Eclipse has the
equivalent).
On the other hand, there’s the Ruby-Doc.org Web site, which is incomplete but useful. Here’s why: for every method in every class, there’s a View source button, and as I’ve already remarked, Ruby source is unusually readable, and at the end of the day the source is the Ultimate Truth about what the software really does. So, on balance, it’s pretty good. Here’s an example of an important method that’s completely undocumented but the source tells you what you need to know.
What’s Lame · Damn, I miss my IDE. I am proficient in two IDEs, Emacs and NetBeans, and neither of them does auto-complete on my Ruby code. It is deeply wrong when characters that could in principle be supplied by a computer must be typed by a human, and deeply wrong when there’s an obvious syntax error in your code and your editor doesn’t let you know. I gather that both Eclipse and TextMate can be made to be smart about Ruby. I’d really like to avoid having to learn a new editor. Sigh. Anyone in Prague listening?
Ruby has a kind of stand-offish attitude towards two of my favorite pieces of infrastructure, XML and Unicode. REXML provides a nice API, but, as Sam Ruby discovered, has big-enough holes that you can’t point it at Arbitrary Internet XML and hope for good results.
I’m still learning about the Ruby/Unicode issues, but for the moment, suffice it to say that the most advanced Ruby regex technology doesn’t do Unicode properties. Blecch.
Finally, a minor, almost a footnote, issue. I miss polymorphism. In particular, I miss polymorphic constructors. Sometimes you make pancakes by taking them out of the freezer and thawing them. Sometimes you use baking power, flour, and eggs. Sometimes you use a mix (and there are some excellent pancake mixes available at your local organic-food retailer). Software’s like that, too. I find Ruby’s argument lists (required args, optional args, an array, &block) kind of ad-hoc and kludgey. But if you embrace the virtues of duck typing (and I do), I guess you pay a price in polymorphism and that’s How Life Is.
Conclusions · See above. But, now that I’m getting comfy with Ruby, I’m wondering why I need to use any frameworks at all for my comment system. Templates, ORM, MVC; bah, who needs ’em?