declaration for module methods which need to be implemented

P

Paul

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
but is there a programtaic way to declare this?
end

end
 
T

Trans

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
but is there a programtaic way to declare this?
end

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

Is there a way to enforce that the 'children' method is understood in
any class which includes this module? Or do I simply rely on a
comment, as above

You general just make a comment in the module docs. Ie.

# The including module must define #children.
#
module Parenting
...
end

Under certain conditions you might provide a default, or raise an
error, eg.

module Parenting
def children
raise NoMethodError
end
end

T.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,274
Messages
2,571,366
Members
48,051
Latest member
noblesalt

Latest Threads

Top