Thursday, April 05, 2007

Understands and Groks in Ruby

When I fashioned an approximately_equals method for Numeric in Ruby, I missed something. I want to not get epsilon (the maximum difference to still be considered equal) from Numeric if a different epsilon can be found in a derived class closer to the instance on the inheritence chain. While I'm probably going to do this through an include, it did turn me on to a chain of mail that was talking about the use of include at the instance versus the class level in Ruby.

The main point of the discussion wasn't centered around it's value - it is clear that it would be great to be able to mixin at the class level as well as the at instance level. The main point is what to call it. I'll go on the record - I don't like "include", the method name that mixes a Module's instance methods into the includer. And though there were many suggestions for class-and-instance-level inclusion, most didn't connote the intention or were syntactically cryptic.

What I already have done with "include" is this: in a centrally required class, I alias it to "understands". My rationale is, if Module Foo is included in Class Bar, the instances of Bar understand Foo - they can do what Foo has defined. In my 3D classes, for instance, I now say:
class Point3
understands ThreeDimensions
end
and it knows all sorts of things about the 3D world.

Once I finally really need it and take the time to do a little metaprogramming (or if it finally comes out as a Ruby 2 feature and I get it for free) I'm going to do the same thing for the class level. But I'll call it "groks", which connotes a deeper understanding than "understands". When Class Foo groks Module Bar, The Foo Class can do what Bar defines.

Grok belongs to Robert A. Heinlein, from his book "Stranger in a Strange Land". Its a made-up word that means "to drink". The usage goes further though, to the concept of "more than understanding", a capture of essence, a blending into identity - which is exactly what mixing in at the class level is all about to me.

No comments: