From: Simon Strandgaard <0bz63fz3m1qt3001@...> Date: 2002-10-21T16:53:10+09:00 Subject: Re: Duck patterns in Ruby On Mon, 21 Oct 2002 02:41:53 +0200 "MikkelFJ" wrote: [snip] > So I thought it would be nice to be able to branch the code based on > whether an argument had the "each" method. This lead to the idea of > a kind of pattern matching based on available methods instead of the > more strict matching on a class. > > def product (x, y) > p = 0 > match_methods(x, y) > when(:each, nil) x.each {|a| p = p + a.multiply(y) } > when(nil, :each) y.each {|b| p = p + x.multiply(b) } > when(nil, nil) p = x.multiply(y) > end > return p > end > > (Might also be nice to branch based on whether a method yields or takes a > code block as argument). > > It is somewhat similar to type matching in languages like OCaml, except here > you match the duck type, not the class type. Yes, this is pattern-matching ala Standard ML, this is realy nice ;-) also know as multimethods, double dispatch, visitor. [snip] What are your question ? do you want multimethods ? regards Simon Strandgaard