From: transfire@... Date: 2006-07-25T02:49:28+09:00 Subject: Re: I'll have the duck! Daniel DeLorme wrote: > benjohn@fysh.org wrote: > > I also like it because I don't think that there's anything especially > > fundamental about classes. With predicate classes, all you're really > > saying is that "if I've got something that can do x and y, then I know > > that I can equally validly think of it as something that can do z". > > The concept really piqued my interest, and since ruby gives us such nice > metaprogramming abilities, why not do it? > > module DuckTyping > @@quacks = Hash.new{ |h,k| h[k] = {} } [snip cool code] > ducktype :quack do > def quack_loudly > quack.upcase > end > end > > > class Duck > def quack > "quack!" > end > end > > class Dog > def bark > "woof!" > end > end > > Duck.new.quack_loudly #=> "QUACK!" > Dog.new.quack_loudly #=> NoMethodError > > > nifty? Nicely done! Code worthy of experimentation. A lot of excellent comments in this thread too. I wonder then, if we take up John Carter's notion on duck as mixin, then Enumerable can be defined as: ducktype :each do def collect each{ |e| yield(e) } end ... end Although clearly the distinction between a mixin and a ducktype is the ducktypes global influence. Quite powerful! Yet, I do imagine that with this is place someone would call for selector namepsaces to reign in the abundant flocks ;) Onefurther step would have to be taken, at the very least. a means of constraining them to specific scopes. Perhaps that's a simple as limiting them the module space they are defined in? In any case very interesting. I really wonder just how far one can take this shift in paradigm? T.