Ruby Elegance Nuances

July 28th, 2007

I’ve recently been doing some very fun, elegant, and exciting things with Ruby at work and in my personal time. Although I have to admit that I learned Ruby because of the amount of hype Rails got on Digg back in the day, I am proud and overjoyed to consider myself a Ruby developer now.

Even though these bits of knowledge are obvious to advanced Ruby developers, I think there is value in recognizing these in context and expanding upon them. Below is my version of a summary of a presentation, courtesy Glenn Vanderburg.

  • Punctuation gets in the way for short lines, but makes long lines more readable (this applies to the use of single versus multi-line blocks too.)
  • The minimal syntax to open and close blocks makes their use feel natural. This naturalness is powerful, because blocks seem to allow powerful abstractions to be developed without much thought.
  • Sigils are used for scope (@@, @, CAPS, $, etc) rather than typing (like in Perl.) This use of sigils helps, because the most common scopes (classes and local variables) do not have them. However, they provide an effective language feature that eliminates an annoying standard like using an underscore before instance variables (Java.)
  • There are usually two (or possibly more) ways of doing something, allowing for the coded implementation to fit the context.
  • Mixins/modules cannot create instance variables (because they cannot be instantiated), but they can reference them (because the instance variables spring into life inside of the object the module is mixed into.)
  • The Enumerable module, along with blocks, allows Ruby to apply some functional programming techniques.
  • Operator overloading.
  • “Three-quals” (===) allows for beautiful case statements by defining the concept of a match.
  • “Open classes” allow for nice common interfaces and DSL’s to be defined when patterns are discovered (ie: Rails’ blank? method on NilClass and String.)
  • Classes being defined can execute code on themselves to define themselves further because they are objects.
  • Although classes are just object instances, creating them still uses a declaration (unlike Smalltalk) and feels natural.

As a closing note, Ruby makes trade-offs in its design like any other language. These range from dynamic typing (which makes tool creation harder), to the “Principle of Least Surprise” (which make consistency harder), to many other things.

Glenn points out, and I agree, that Ruby seems to strike all the right balances (big ups to Matz.) However, this is a personal preference and we should respect the languages choices of others as a matter of taste… there’s room for everyone =)

One Response to “Ruby Elegance Nuances”

  1. Josh ben Jore Says:

    I left this note on the infoq page as well. This is in response to a several minute tangent into the usefulness of having regexps objects and their non-existence in Perl.

    Perl has had a regexp object since 5.6 was released in 2000. I don’t know of any operating systems that still ship with an older version. The speaker should have done his homework on this before spending what seemed like five minutes larking about how this feature was lacking.

    We’ve had this seven years now. Please catch up.

Leave a Reply