ClassMethods vs append_features/extend self

Thoughts on this:

module Foo

  def self.append_features(base)
    base.extend self
  end

  def foo
    # ...
  end

end

I have done the above at times, as opposed to the alternative:

module Foo

  def self.append_features(base)
    base.extend ClassMethods
  end

  module ClassMethods
    def foo
      # ...
    end
  end

end

b/c I never liked having this non-descript module about --it feels
extraneous.

The downside of the former approach is that there is no way to ever
actually include the module’s methods, but for the purposes of the
program that’s almost certainly a YAGNI.

Any other downsides?

Thomas S. wrote in post #996028:

Thoughts on this:

  1. I would use included() rather than append_features() because it’s a
    more descriptive name for the event you are trying to hook into.

  2. The second form is more versatile because you can use the module to
    store both regular methods and class methods that are to be included.

  3. The second form makes it easier to understand what is actually
    happening: it doesn’t use ‘self’ and it contains the descriptive name
    ‘ClassMethods’.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs