Mind blowing Ruby

I’ve been writing Ruby code for over four years now. I’ve been getting paid to do so for about two years. Prior to that, the bacon I brought home came mainly through the careful crafting of lines of Java, PHP, and C# code. Matz spoiled the party for me in the mid-90s, however, by releasing such a wonderfully beautiful language that it made Java tedious and uninteresting in comparison.

Ruby is a fantastic tool. But it’s not just a tool, it’s also an art medium. I had never truly seen beautiful code until I met Ruby. All that being said, when I was first introduced to the language it was a lot like trying to breathe while standing under Niagara Falls. The “wow-that’s-cool-but-I-don’t-quite-grok-it” moments came fast and furious. Ruby was blowing my mind on a daily basis.

It doesn’t anymore. At least, not every day. But at least once a month, I see a code snippet or read a blog post or hear about a new way to do something and… well, there goes my mind again. For example…

Classes Are Constants

The fact that classes are constants in Ruby doesn’t seem particularly remarkable on the surface, but when you need to do something like this it sure comes in handy:

Once I grokked the usefulness of classes and constants being interchangeable, I began coming across lots of places in my code where I was able to refactor a complex bit of logic into a shorter, more simpler form. That’s one of the neat things about Ruby: once you learn a given Rubyism, it can be applied all over the place.

Iterating Over Collections

Let’s face it: iterating over a collection in Java, or even in C#, is bulky and painful. When I ran across Ruby’s beautiful Enumerable module, my mind was once again totally blown by the simplicity and elegance:

I’ve never been much of a fan of Perl. I’m guessing something similar can be done there. But for me, a young developer fresh out of college, the ability to iterate (not to mention define an array or a hash) in a single line of code was a big draw away from my world of statically typed languages. The readability of Ruby’s syntax is immensely gratifying.

Ruby’s Vibrant Community

Perhaps the most mind-blowing thing of all is the Ruby community itself. I have never come across a more intelligent, passionate, and close-knit group of developers as those who call Ruby their language of choice. The wild Rubyist used to be a rare breed, but we’re quickly becoming more common as Ruby’s fame continues to spread. Whether I’m meeting new folks at the local Ruby Meetup, doing some late night hacking in Chicago at RailsConf 2006, or just chatting in Campfire with fellow Terraliens, the openness and technical savvy of these people is stunning.

It’s truly a humbling experience to be part of this movement. And that’s really what it is: a movement aimed at refactoring the norms of software development. Ruby and the frameworks built with it continue to grow and evolve. The community, myself included, grows and evolves along with it.

Here’s hoping that Matz, DHH, and the other brilliant people behind these wonderful technologies continue blowing our minds with creative innovations for years to come.

But for me, a young developer fresh out of college, the ability to iterate (not to mention define an array or a hash) in a single line of code was a big draw away from my world of statically typed languages.

Being statically typed has nothing to do with it, languages like Haskell, Standard ML and OCaml are just as statically typed as Java/C# (and without all those tedious type annotations all over the place too). And you can do just the same one liners as in Ruby, here is an example in OCaml:
let animals = [ "dog"; "cat"; "bird" ] in
List.iter (fun a -> print_string(a ^ " ")) animals;
print_string (List.filter (fun a -> Str.string_match (Str.regexp_string "do") a 0) animals);

The regexp code is a little messier because it does not have the syntactic sugar that Ruby and Perl do for them, but this is essentially the same thing and it is statically typed.

Thanks for the code samples, Stevan. The Perl sample is especially interesting.

I didn’t mean to imply that statically typed languages can’t support iterators with shortened syntax. I was simply using the phrase “statically typed” to refer to the group of languages I had been taught in college. Sorry if I wasn’t clear.