Hi --

On Wed, 30 Nov 2005, Trans wrote:

> I'm sure everyone knows about the ClassMethods technique for getting a
> module's class/module methods to be inherited.

I didn't know there was a widely-known or standard technique of that
name (and I wouldn't use the term "inherited" to describe what happens
to methods in modules; it muddies the waters a bit), but if you mean
the kind of thing that's done in ActiveRecord and related libraries to
promote instance methods of a module to pseudo-class-methods of
classes that include the module's containing module, then yes :-)
(I say "pseudo" because they're not actually class methods, but they
end up being useable as if they were.)

> I have come up with an
> improvement on this, but rather than define a ClassMehthods module
> manually one supplies a block to a method. It still creates the
> ClassMethods module, but it does so using the block. If there were a
> way to hook into module creation that wouldn't be neccessary. But is
> there a way to hook into this?
>
> On the other hand sticking with the block method, I realized I could
> use a capitalized method name without inherfereing witht the module
> (they exist in separate namespaces). So..
>
>  module Foo
>
>    ClassMethods do
>      def x ; 1 ; end
>    end

I think the pseudo-ness of the class method-ness of these methods is
an issue here.  You're really redefining the notion of a class method;
it's no longer a singleton method of a class object.  I imagine
some counter-argument involving the word "duck" could probably be
developed... :-)  But I'm personally not a big fan of just asserting
that something is a class method when that's really not the right term
for it.  (I have the same problem with the ClassMethods module
technique.  It works by a kind of consensus, but I don't consider it
an optimal naming scheme.)

>  end
>
>  class Bar ; include Foo ; end
>
>  Bar.x  #=> 1
>
> Using a capitalized method like this, is it a good idea b/c it
> corresponds to the module name, or a bad idea b/c it does against
> convention?

I'm confused -- do you have a ClassMethods module lurking somewhere?


David

-- 
David A. Black
dblack / wobblini.net