ClassMethods vs append_features/extend self

Discussion in 'Ruby' started by Intransition, May 1, 2011.

  1. Intransition

    Intransition Guest

    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?
     
    Intransition, May 1, 2011
    #1
    1. Advertisements

  2. Intransition

    7stud -- Guest

    Thomas Sawyer wrote in post #996028:

    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.
     
    7stud --, May 2, 2011
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.