[Nitro] A better #is ?

Trans transfire at gmail.com
Fri Jul 27 10:54:23 EDT 2007



On Jul 27, 7:35 am, "Jonathan Buch" <j... at oxyliquit.de> wrote:
> Hi,
>
> >   Bar.x  #=> "x"
>
> Strikes me as redundand with class_extension, too much power for `is`?

Not redundant, but #is could be made smart enough to recognize a
module that has a ClassMethods section and thus work as a suitable
alternative to class_extension. I kind of like that idea myself,
though I've always thought "ClassMethods" was a poor name choice.

I guess the debate is between the side that says, all modules should
be essentially equal and the method (include or extend) determines
it's use, vs. the side that says, the module should be a pure
encapsulation of functionality and thus dictate the terms of its own
use.

The things about implementing #is this way, it allows both options.
Use #include/#extend for the first, #is for the second.

> I mean, you already have extend and include, having a hybrid by
> inventing a lengthy `def self.append_feature_function` somehow isn't
> so appealing to me.

That's true. And actually one could use the callback:

  module Foo
    def append_feature(mod)
      mod.extend self
    end

    def x; "x"; end
  end

  class Bar
    is Foo
  end

  Bar.x  #=> "x"

However, that sort of misleads the programmer. #include is not
actually happening at all. In fact, one could never make that module
include even if they wanted too, without undefing append_feature.
Which is why I thought #is a good compromise. But perhaps there's a
bit more elegant solution using another callback?

T.



More information about the Nitro-general mailing list