Ricky, Ruby and Rails (hypothetically speaking)

In the bits of spare time I get here and there, I’ve been continuing my hypothetical hunt for a language and web framework in which to implement my hypothetical "web 2.0" idea. It occurs to me that if all these little bits of spare time were clumped together so that I could, hypothetically, do some actual coding as opposed to “investigating”, hypothetically I’d be well on my way to having a hypothetical working system by now. But, alas, little bits of time here and there is all I’ve got at the moment.

chunky bacon

Anyway, after having checked out Python and Django and deciding that I’d be happy enough with that set of tools, I thought I’d better check out Ruby and Ruby on Rails to see what all the fuss is about. Well, I have to say, so far I really like the Ruby language. I’ve been helped along by what has to be the weirdest, but coolest, guide to programming that has ever been written for any language (what other programming guide comes with cartoon foxes and its own soundtrack?). I’m still learning the ins and outs of Rails, but there are some very helpful tutorials online for this, too. The fact that Ruby, like Python, comes pre-installed in Leopard was a pleasant surprise. Ruby comes with a command shell environment called irb (Interactive Ruby), which enables you type Ruby code at the command prompt (again, just like Python’s python command line tool). This makes it very easy to experiment with the language.

One of the things I like about Python is list comprehensions. They’re a very neat and convenient way of mapping a list to a new list by applying a function to each element of the original. It kind of works like the map function in many other languages, except you can include conditional statements. The heavy use of list comprehensions in Programming Collective Intelligence tells me that there’s a good chance they’ll come in handy for me later on. Here’s a trivial example:

>>> list1=[1,2,3]
>>> list2=[x**2 for x in list1 if x%2==1]
>>> list2
[1, 9]

In Ruby there’s no syntactic sugar for list comprehensions, but it turns out you can pretty easily implement the required behaviour:

>> list1=[1,2,3]
=> [1, 2, 3]
>> list2=list1.map {|x| x**2 if x%2==1}.compact
=> [1, 9]

Furthermore, since classes are never “sealed” or “final” in Ruby, it means that you can do something like this:

>> class Array
>>   def comprehend( &block )
>>     block ? map( &block ).compact : self
>>   end
>> end

We’ve just added another method to the (existing) Array class which works very much like a list comprehension:

>> list1.comprehend {|x| x**2 if x%2==1}
=> [1, 9]

That’s a little of what I’ve learned about Ruby in the past week or so. Anyway, I can say that I’ve narrowed down my search to two candidates. Python has the lead in terms of being a mature technology. But Ruby really is fun to program in, and I like its syntax better than Python’s. Furthermore, I spent a while faffing around with Django and mod_python for my Mac. But getting Rails up and running was a breeze using the Mongrel web server – a production quality web server for Ruby applications, used by many web sites including Twitter. Ultimately, my (hypothetical) first hire gets the final say. :-)