From: "Paul Brannan" <pbrannan / atdesk.com>


>
> > I don't see the need for interfaces in a language with mix-ins.
>
> This doesn't make sense to me.  I see interfaces and mixins as
> orthogonal concepts; interfaces are used to specify a contract, while
> mixins are used to provide additional methods to a class.
>
> Paul

The confusion arises because if you mix in a module, it effectively becomes
part of the inheritance chain.  For instance:

  [1,2,3].is_a? Enumerable
  ('a' .. 'z').is_a? Enumerable
  "dog".is_a? Enumerable

Thus, the methods of Enumerable can be sent to String, Array, and Range.  This
is like a Java interface.  More specifically, it's like *one* use of an
interface: part of the built-in library advertising capabilities, like
Comparable, Runnable, etc.

The main use for Java interfaces in user code is for business objects, for some
definition of "business".  You learn something about the objects by looking at
the interfaces, whereas with mixins, because they provide functionality, and
the ingenious one Enumerable provides really cool functionality for you for
free, can only tell you what the capabilities of an object are, not much about
its use.

I've actually never written a module, but I believe they are meant to be spread
functionality far and wide, whereas Java interfaces are basically for
modelling.  There simply is no equivalent in the Ruby language, and do I care?
No.

The original poster should search www.ruby-talk.org for "java interface" if he
wants to see more discussions.  (I haven't tried it, but it's sure to return a
goldmine.)

Gavin