From: jjenning@... (jared l. jennings) Date: 2001-07-24T09:11:10+09:00 Subject: [ruby-talk:18397] Re: interfaces? On Tue, Jul 24, 2001 at 08:49:16AM +0900, MikkelFJ wrote: > 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 umm, can you just give everything a clip method? i don't see why not. i think the reason has to do with this intersection thing. anyway read on. > 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. Well, look at this. jaredj@cobalt:~$ cat foo.rb class Foo def bar print "bar\n" end def baz print "baz\n" end end module Mix def baz print "mixed!\n" end end f = Foo.new f.bar f.baz f.extend Mix f.bar f.baz print "f is a Foo!\n" if f.is_a? Foo print "f is a Mix!\n" if f.is_a? Mix jaredj@cobalt:~$ ruby foo.rb bar baz bar mixed! f is a Foo! f is a Mix! > 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 if you were to make such contract-modules, you could also go like: supporting = collection.find_all {|thing| thing.is_a? ContractModule } supporting.each { ... } -- Doubt is a pain too lonely to know that faith is his twin brother. - Kahlil Gibran