Learning Ruby : Class methods != Static Methods

26 Jun 2010

Yesterday I blogged about the general differences between classes in ruby vs classes in c#/java. I later found a quote I had been looking for from Scott Bellware which I think does a good job explaining the difference:

In an object-oriented language, objects are defined by defining objects rather than classes, although classes can provide some useful templates for specific, cookie-cutter definitions of a given abstraction. In a class-oriented language, like C# for example, objects must be defined by classes, and these templates are usually canned and packaged and made immutable before runtime. This arbitrary constraint that objects must be defined before runtime and that the definitions of objects are immutable is not an object-oriented concept; it's class oriented.

Changing how we see and think of classes isn't easy, but it is essential to mastering ruby.

I also wanted to bring up something else Scott shared with me with respect to static classes. In my previous post I said that what we call static methods in C#/Java are known as class methods in ruby. At best, this is an over simplification. Its true that class methods in ruby are the closest thing to static methods, but there's more to them than just that. Since class methods are actually defined on an object, it is important to think of them as instance method (belonging a metaclass) - because thats what they are.

The point? Don't think of class methods in ruby as static methods. Class methods ARE instance method (to prove it, they can be overwritten).

blog comments powered by Disqus