# Enforce implementation of Module method

**URL:** <https://www.ruby-forum.com/t/enforce-implementation-of-module-method/123096>\
**Category:** Ruby\
**Created:** [December 13, 2007, 11:51pm UTC](https://www.ruby-forum.com/t/enforce-implementation-of-module-method/123096 "2007-12-13T23:51:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [December 13, 2007, 11:51pm UTC](https://www.ruby-forum.com/t/enforce-implementation-of-module-method/123096/1 "2007-12-13T23:51:17Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [December 14, 2007, 1:43am UTC](https://www.ruby-forum.com/t/enforce-implementation-of-module-method/123096/2 "2007-12-14T01:43:17Z")

</div>

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

> ```
> self.children.delete(a_child)
> 
> ```
> 
> 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

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [December 14, 2007, 3:59pm UTC](https://www.ruby-forum.com/t/enforce-implementation-of-module-method/123096/3 "2007-12-14T15:59:43Z")

</div>

Phrogz \<phrogz [mac.com](http://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.

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [December 14, 2007, 1:50am UTC](https://www.ruby-forum.com/t/enforce-implementation-of-module-method/123096/4 "2007-12-14T01:50:55Z")

</div>

On Dec 13, 3:38 pm, Paul [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> self.children.delete(a_child)
> 
> ```
> 
> 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.

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [December 14, 2007, 4:19pm UTC](https://www.ruby-forum.com/t/enforce-implementation-of-module-method/123096/5 "2007-12-14T16:19:39Z")

</div>

On Dec 14, 2007, at 9:58 AM, Gareth A. wrote:

> > 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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
