Enforce implementation of Module method

Discussion in 'Ruby' started by Paul, Dec 13, 2007.

  1. Paul

    Paul Guest

    Let's say I have the following module:

    ------------------------
    module Parenting

    def add_child(a_child)
    self.children.push(a_child)
    end

    def delete_child(a_child)
    self.children.delete(a_child)
    end

    def children
    # need to implement
    end

    end
    ------------------------

    Is there a way to enforce that the 'children' method is implemented in
    any class which includes this module? Or do I simply rely on a
    commenting convention, as above?
     
    Paul, Dec 13, 2007
    #1
    1. Advertising

  2. Paul

    Ken Bloom Guest

    On Thu, 13 Dec 2007 14:38:34 -0800, Paul wrote:

    > Let's say I have the following module:
    >
    > ------------------------
    > module Parenting
    >
    > def add_child(a_child)
    > self.children.push(a_child)
    > end
    >
    > def delete_child(a_child)
    > self.children.delete(a_child)
    > end
    >
    > def children
    > # need to implement
    > end
    >
    > end
    > ------------------------
    >
    > Is there a way to enforce that the 'children' method is implemented in
    > any class which includes this module? Or do I simply rely on a
    > commenting convention, as above?


    It may be the more ruby way to rely on the commenting convention (there's
    no need to define the children method at all in Parenting, just comment
    somewhere that it needs to be implemented.)

    But if checking is really a must, then you can check from
    Parenting.included as follows:

    module Parenting
    def self.included klass
    raise NoMethodError, "#{klass} must define #children" unless
    klass.method_defined? :children
    end
    end



    --
    Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
    Department of Computer Science. Illinois Institute of Technology.
    http://www.iit.edu/~kbloom1/
     
    Ken Bloom, Dec 14, 2007
    #2
    1. Advertising

  3. Paul

    Phrogz Guest

    On Dec 13, 3:38 pm, Paul <> wrote:
    > Let's say I have the following module:
    >
    > ------------------------
    > module Parenting
    >
    > def add_child(a_child)
    > self.children.push(a_child)
    > end
    >
    > def delete_child(a_child)
    > self.children.delete(a_child)
    > end
    >
    > def children
    > # need to implement
    > end
    >
    > end
    > ------------------------
    >
    > Is there a way to enforce that the 'children' method is implemented in
    > any class which includes this module? Or do I simply rely on a
    > commenting convention, as above?


    module Parenting
    def children
    raise "OOPS!" #Better error message here
    end
    end

    If a class defines that method, it will shadow the module method. As
    long as the class method doesn't try to call super, you should be good
    to go.
     
    Phrogz, Dec 14, 2007
    #3
  4. Paul

    Gareth Adams Guest

    Phrogz <phrogz <at> mac.com> writes:
    > module Parenting
    > def children
    > raise "OOPS!" #Better error message here
    > end
    > end
    >
    > If a class defines that method, it will shadow the module method. As
    > long as the class method doesn't try to call super, you should be good
    > to go.


    And of course, there's a rather handy NotImplementedError class just sat there
    in Ruby code if you want to use it.
     
    Gareth Adams, Dec 14, 2007
    #4
  5. On Dec 14, 2007, at 9:58 AM, Gareth Adams wrote:
    > Phrogz <phrogz <at> mac.com> writes:
    >> module Parenting
    >> def children
    >> raise "OOPS!" #Better error message here
    >> end
    >> end
    >>
    >> If a class defines that method, it will shadow the module method. As
    >> long as the class method doesn't try to call super, you should be
    >> good
    >> to go.

    >
    > And of course, there's a rather handy NotImplementedError class just
    > sat there
    > in Ruby code if you want to use it.


    If you simply don't define Parenting#children, you'll get a
    NoMethodError. Isn't that good enough?

    -Rob

    Rob Biedenharn http://agileconsultingllc.com
     
    Rob Biedenharn, Dec 14, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mehdi
    Replies:
    2
    Views:
    419
    Mehdi
    Jan 29, 2005
  2. Niclas

    How to enforce registration ?

    Niclas, Mar 27, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    353
    Steve C. Orr [MVP, MCSD]
    Mar 27, 2006
  3. guava
    Replies:
    1
    Views:
    519
    Gordon Beaton
    Jul 11, 2003
  4. Jeff
    Replies:
    3
    Views:
    666
    Roedy Green
    Jul 31, 2003
  5. Mize-ze

    enforce override of method

    Mize-ze, Jun 25, 2007, in forum: Java
    Replies:
    11
    Views:
    594
    Philipp Leitner
    Jun 26, 2007
Loading...

Share This Page