Does the open class nature of Ruby effect how we think of
classes? Perhaps it does.
More on the Array API Controversy
I commented earlier on the Array API controversy (see Array
API). I had
some more thoughts on that topic.
As both a Java programmer and a Ruby programmer, I enjoy comparing
the two languages, noting their differences and more importantly,
recognizing the different ways they effect my thinking.
In general, I would tend to agree with Elliotte on creating classes
with small APIs. They do seem to be easier.
On the other hand, I’m not really bothered by the large Array API
provided by Ruby. There is a clash between what I believe and what I
find to be true.
Perhaps some of the answer lies in the open nature of Ruby classes. A
Ruby class can be extened at any time by any user. Although the
potential for abuse exists, it is also a very handy thing to be able
As a result, methods tend to migrate to the classes they belong to,
rather than put in a utility method somewhere. Although I might never
use assoc, if I were to look for it, I would start in the Array
class1, or one of its included modules.
Since rubyists accept the fact that we can’t ever completely know a
class, the fact it has extra methods on it that we aren’t using
doesn’t seem to be a big deal.
Java, on the other hand, has closed classes. The methods available on
List are all that are there, and all that ever will be there (well,
until someone modifies and recompiles the class). This gives the
impression that I can fully understand the class and exactly what it
is doing. This impression is further strengthened by the use of
interfaces, which further narrows down the user’s view of a class to
just the methods available in an interface.
I don’t know, maybe this has nothing to do with it. And there are
certainly other factors involved. For example, Array is such a
commonly used class2 that perhaps this is the exception to the rule
of small interfaces.
Whatever the reason, I don’t find using Array objects to be a burden
at all, despite what the theory of small interfaces might indicate.
1 In fact, I did need assoc recently. Guess where I found it.
2 I find that Array is used much more in Ruby than any variety of
List is used in Java. I don’t know if that a good thing or not.