Hi,

This may be somehow stupid but what about simply inheriting
from Hash ?

In the worst case you aren't going to use the hash default
implementation at all, because you would provide your own
implementation for the storage. But that case is not that bad,
because I suspect that the overhead is very very small.

class MyObject < Hash
  def [](key)
    my_ultra_efficient_get( key)
  end
end

Then you can kind_of? Hash as usual.

End-of-trick

That does not solve the more general issue where Interface
is distinct from Implementation.

OTOH I believe that ducktyping is not about Interfaces.
To me, it is more about service providers not checking
types and handling exceptions in a gentle way. What
the service *provider* expects is part of the contract and,
as an example, requiring a Hash is much less tolerant
that requiring something that can [](key) and []=(k,v).

If the service *user* does not respect the contract, it is
its responsability, not the provider's one.

i.e. If you provide something that does not implement
[](key) then don't expect thing to work.

As a provider, you want to make it clear that it is
the user that did not respect the contract, hence the
need for good exception handling.

Yours,

Jean-Hugues
-------------------------------------------------------------------------
Web:  http://hdl.handle.net/1030.37/1.1
Phone: +33 (0) 4 92 27 74 17