Sam Kong wrote:
> If the method only exists in a library class, how do you know what it
> would do to your class? It sounds like the best way to get the effect
> you want might be the same thing you would do in C++ or Java, viz. to
> let C2 have a member of type C1, and forward method calls to the member
> object according to the "Law" of Demeter.
>
If this is possible, it is a better solution of course. I think in
general it's better to use delegation than to use inheritance for code
sharing purposes, using modules is maybe on par with delegation.
>
> Yes, I think you're entirely right on this.
> If C2 doesn't know how C1#f is implemented, it's no used copying the
> method's implementation or even copying the implementation won't work
> in C2's context.
> Now I know that my imaginary problem was totally non-sense.
>
> Thank you for enlightening me.
>
Ok, let me switch off the light again.

You would read the source code
before you do it and decide if you can prepare instance variables and
methods in the required way. I would call that "internal duck typing".
Of course this would make it necessary to study the source code of C1 to
find out what exactly it does. But this isn't so different from what you
have to do if you want to inherit from the class (especially in Ruby).
--
Florian Frank