My preference for ruby
I’m often drawn to program in the easiest language that will do what I want. Some of this is just run-of-the-mill programmer’s laziness, but mostly it’s because ruby comes as close as any language I’ve found to expressing how I think of algorithms. Only slightly secondarily is that it’s one of only two languages I’ve worked with, and the only object-oriented one, in which I’ve routinely been able to drop down into the middle of someone else’s uncommented code and understand what it’s doing with enough confidence to make modifications. This is huge to me. Code is only written once, and continuously maintained after that, sometimes years later. There are a number of different techniques for dealing with maintaining code that other people have written - documentation and commenting standards, logical interfaces and encapsulation, pair programming, etc… - but by far the easiest one is simply being able to read it and understand what it does. Ruby encourages this in a way that other languages do not. Sure, it’s possible to write ruby code that’s not understandable, but it’s more difficult, and it’s usually (but not always) a signal that you’re doing something wrong.
Ruby is elegant, and it takes its commitment to being object-oriented seriously. In ruby, everything is actually an object, even low-level datatypes like integers. Combined with the block structures that let you pass bits of code around, this is both very powerful and expressive (which translates into readable). It’s a simple one, but I like this example, which shows off both this iterator concept and ruby’s type awareness:
» x = “yes”
=> “yes”
» 3.times {|n| puts n.to_s + x}
0yes
1yes
2yes
» 3.times {|n| puts n.to_s + x * n}
0
1yes
2yesyes
I like that the object methods are well thought out and consistent. Again, it’s a simple example, but it pleases me to no end that the join method is part of the array type and not the string type.
Here’s how you concatenate an array into a comma-separated string in python:
» numlist = [1,2,3,4]
» numlist
[1, 2, 3, 4]
» “,”.join([str(i) for i in numlist])
‘1,2,3,4’
In ruby, this is much more concise:
» numlist = [1,2,3,4]
=> [1, 2, 3, 4]
» numlist.join(‘,’)
=> “1,2,3,4”
Just about the only place that ruby falls down for me is in raw performance, though that looks set to improve significantly with ruby 1.9 and 2.0, which I’m eagerly awaiting. For the time being, I’m still using python in places where I need more speed but still need the rapid application development turnaround time of a scripting language that would preclude writing it in java or C (I’ve long since largely given up on perl as being unmaintainable as soon as its written).
For me, in most cases, the extreme readability and ease of coding far outweighs any drawbacks. I’ll post more examples as they come up - this is the tip of the iceberg.