A few weeks ago Martin Fowler started a miniature firestorm by putting an entry in his bliki about Humane Interfaces. The irony was that the negative opinions it generated were not so much about humane versus minimal interfaces, but about the fact that Ruby’s Array class has 78 methods versus the 25 declared in Java’s java.util.List class.
The main critic of Ruby’s Array class was Elliotte Harold, who among other things called it “about three times as bad as a 25 method List class.” The thing is, as a Java programmer I can understand Mr. Harold’s arguments, and I think they might be valid in Java. But they are not valid in Ruby. You see programming in Ruby is a completely different experience to programming in Java. I should know, as I like to say I was “born and raised” on Java (it was my first major programming language), yet I’ve also been programming Ruby since 2001.
One of the biggest differences between the two languages is Java’s static versus Ruby’s dynamic typing. This is also one of the biggest reasons a 78 method Ruby class is perfectly fine in Ruby (I would go so far as to say wonderful) whereas the same in Java might be a nightmare. Let’s take a look at a code example (from IRB, aka Interactive Ruby):
irb(main):001:0> list =  =>  irb(main):002:0> list << 1 =>  irb(main):003:0> list << "two" => [1, "two"] irb(main):004:0> list => 1 irb(main):005:0> list.last => "two" irb(main):006:0> stack =  =>  irb(main):007:0> stack.push "a" => ["a"] irb(main):008:0> stack.push "b" => ["a", "b"] irb(main):009:0> stack.pop => "b" irb(main):010:0> queue =  =>  irb(main):011:0> queue.unshift 1 =>  irb(main):012:0> queue.unshift 2 => [2, 1] irb(main):013:0> queue.shift => 2
Here I have list, stack and queue data structures. Yet they are all really Ruby Arrays. Nowhere above do I need or care about what actual class these objects are, I just call the methods I expect and things just work. This is frequently called “duck typing” in the Ruby community (and I’m sure other places), because as long as it quacks and walks like a duck, we are happy. In other words, an object only need respond properly to a given set of methods to satisfy the programmer’s requirements. Whether it is an Array or a QuizzleBick under the scenes is irrelevant.
This is where Mr. Harold’s Java prejudices show most obviously, and also it is clear as crystal that he is not a Ruby programmer, and he just cannot “get” what it means to be one. He is so upset by Ruby’s Array class behaving like a List and Stack and Queue that he fails to see how perfectly this idea fits in with Ruby and the way it is programmed.
In the same way that learning new programming languages can open one’s mind, it seems using one for too long can close your mind as well.