From: MikkelFJ Date: 2001-07-24T08:49:16+09:00 Subject: [ruby-talk:18395] interfaces? I am fairly new to Ruby (the DDJ generation), so I may have missed something blatantly obvious: Are there interfaces in Ruby? If not, why not? I like mixins because they avoid problems with multiple inheritance, but unlike interfaces in Java and C#, they actually provide an implementation. However, I would like to be able to query an object about an interface, such that I know I can trust it to do the expected. shape_collection.each do |obj| case obj when IRayIntersection clip_using_interface(obj) when Box clip_as_rectangle(obj) when Sphere clip_as_circle(obj) end end This works for classes, but if you mixed in some module, you cannot test for the functionality provided by that module. You can of course include some identifiers, but you cleanly test for this (unless I'm mistaken of course). Being able to query for a block of functionality i.e. an interface or a module, is important because it has the same advantages as inheritance, but goes beyond the limitations of single inheritance, and doesn't have the problems of multiple inheritance. In the above example you would first try to use the support for built in ray intersection before trying a standard shape intersection algorithm. But in the above, a shape BoxWithHole derived from the Box shape could be supporting the IRayIntersection interface because it a trivial box intersection won't cut it, so to speak. It is not multiple inheritance, it is just an identication that a certain module were mixed in. In this way, a module is much the same as an interface in other languages, except that you don't declare an interface and subsequently implement it. To the earlier discussion on design by contract, one could add the concept of contract to class by mixing in a module that ehh "doesn't exist". It's just a contract, which the object promises to support. Of it doesn't you get a runtime trying to access on of its expected methods. You could then do some fancy iterations over contracts: for each object in the collection supporting this contract.... collection.each_contract(AContract) { ... } or collection.each_module(AModule) { ... } Mikkel