Whenever you bring up the issue of modules vs. classes,
there will generally be at least one person who believes
that including a module is the same as copying its body.
This confusion would go away if the two were unified.
It'd also go away if the rdoc were better:
-------------------------------------------- Module#append_features
append_features(mod) => mod
···
-------------------------------------------------------------------
When this module is included in another, Ruby calls
+append_features+ in this module, passing it the receiving module
in _mod_. Ruby's default implementation is to add the constants,
methods, and module variables of this module to _mod_ if this
module has not already been added to _mod_ or one of its ancestors.
See also +Module#include+.
says nothing about "if method is already defined."
well - classes can't do this
<snip>multiple inheritance</snip>
Why can't classes do that? Is there anything about the nature of classes such that, should we decide to make a change to the Ruby language, it would cause problems if one could "include" classes?
My answer (and I could be wrong) is this: I think the class/module separation is an artificial separation made *intentionally*, to make multiple inheritance hard. Classes and Modules inspire different frames of mind:
* Classes are these self-contained things that need only their constructor called, and are then good to go.
* Modules have no constructor, and are merely a collection of methods, that may or may not be dependent on being included in classes.
I agree that this gap could easily be bridged, such that the Cladule could a)be instantiated, and b)be included, but I believe that it's an artificial limitation in place to limit this sort of confusion:
class SomeDependentComponent
def initialize(name); @name = name end
def do_something
log "component #@name is doing something"
#...
end
end
class SomeMainComponent
include SomeDependentComponent
def initialize(name); @name = name end
def run; do_something end
end
Now, do I think that such an artificial limitation is a good idea? I dunno. Certainly, artificial limitations aren't always good ideas, to me. I like Ruby more than Java, for instance.
Would I prefer not to have this artificial limitation? I think yes. I don't think I'd ever make use of the additional freedom, but I'd be happy with the one less concept to think about.
Devin