Pushing up to Module or Class?

S

Shak

Hi all,

Yes, this question has been asked many times all over the Internet so I'm a
bit embarrassed to admit that I still don't know what the difference is.
Apologies in advance for making you roll your eyes.

If I have a family of classes, each with a common method etc, should I push
that common code up into a superclass or module? Right now I'm inclined to
say the latter since I'll be able to mixin further modules later on if
required. But what would a superclass in Ruby provide that a module
wouldn't? In a ducktyped world I don't see any benefits with
type-polymorphism.

Alternatively: is there any reason why we shouldn't use modules for single
inheritance (seeing as that's a special, trivial case of multiple
inheritance)?

Sorry for boring you all,

Shak
 
R

Robert Klemme

2009/1/28 Shak said:
Hi all,

Yes, this question has been asked many times all over the Internet so I'm a
bit embarrassed to admit that I still don't know what the difference is.
Apologies in advance for making you roll your eyes.

If I have a family of classes, each with a common method etc, should I push
that common code up into a superclass or module? Right now I'm inclined to
say the latter since I'll be able to mixin further modules later on if
required. But what would a superclass in Ruby provide that a module
wouldn't? In a ducktyped world I don't see any benefits with
type-polymorphism.

Alternatively: is there any reason why we shouldn't use modules for single
inheritance (seeing as that's a special, trivial case of multiple
inheritance)?

I tend to view class inheritance in Ruby as "is a" relationship while
module inclusion has a less strict relationship, i.e. the class
including a module inherits particular behavior which does not mean
that it can replace any other class including the module.

IMHO classical inheritance is overrated because there are not so many
places where nested class hierarchies do actually make sense. Often,
delegation is a superior approach because coupling is less strict.

There is probably generally more potential for implementation
inheritance, i.e. sharing common code between multiple classes. The
nice thing is that Ruby allows to do this via module inclusion which
leaves class inheritance for true "is a" relationships.

My 0.02 EUR...
Sorry for boring you all,

Not at all. These kinds of questions can be reasoned about a lot of
time. One reason is probably that there is no clear cut answer to
them.

Cheers

robert
 

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

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,517
Latest member
Andres38A1

Latest Threads

Top