Forum: Ruby Pushing up to Module or Class?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Shak (Guest)
on 2009-01-28 17:12
(Received via mailing list)
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
Robert K. (Guest)
on 2009-01-28 17:45
(Received via mailing list)
2009/1/28 Shak <removed_email_address@domain.invalid>:
> 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
This topic is locked and can not be replied to.